【问题标题】:Creating a set of identical UIImageView subviews创建一组相同的 UIImageView 子视图
【发布时间】:2016-02-21 23:49:22
【问题描述】:

我正在创建一组简单的 UIImageView,它们都使用相同的图像并且在屏幕上垂直均匀分布。最初我只是对每个单独视图的创建进行硬编码,但我想让它更紧凑,因为它们都是相同的图像。

这是我当前的代码,XCode 抱怨 (EXC_BAD_INSTRUCTION)。 loadingView 是一个 UIView,用于将所有图片组合在一起。

let items = [UIImageView?](count:7, repeatedValue: nil)

for item in items {
    item!.image = UIImage(named: "loading_items")
}

// could potentially iterate through this too
self.loadingView.addSubview(items[0]!)
items[0]!.autoPinEdgeToSuperviewEdge(.Top)
self.loadingView.addSubview(items[1]!)
items[1]!.autoPinEdge(.Top, toEdge: .Bottom, ofView: items[0]!, withOffset: 12)
self.loadingView.addSubview(items[2]!)
items[2]!.autoPinEdge(.Top, toEdge: .Bottom, ofView: items[1]!, withOffset: 12)
self.loadingView.addSubview(items[3]!)
items[3]!.autoPinEdge(.Top, toEdge: .Bottom, ofView: items[2]!, withOffset: 12)
self.loadingView.addSubview(items[4]!)
items[4]!.autoPinEdge(.Top, toEdge: .Bottom, ofView: items[3]!, withOffset: 12)
self.loadingView.addSubview(items[5]!)
items[5]!.autoPinEdge(.Top, toEdge: .Bottom, ofView: items[4]!, withOffset: 12)
self.loadingView.addSubview(items[6]!)
items[6]!.autoPinEdge(.Top, toEdge: .Bottom, ofView: items[5]!, withOffset: 12)

for item in items {
    item!.autoAlignAxis(.Vertical, toSameAxisOfView: loadingView) // uses AutoLayout
}

我想知道这段代码有什么问题,以及是否有更高效和“Swift-y”的方式来做到这一点。

【问题讨论】:

    标签: ios iphone swift uiimageview


    【解决方案1】:

    使用 UIStackView 并将您的 UIImageView 添加为堆栈视图的子视图,并将堆栈视图添加为超级视图的子视图。这将使您的工作轻松 //ImageView(view1,view2,view3)

    UIImageview *view1 = [[UIImageview alloc] init];
        view1.backgroundColor = [UIColor blueColor];
        [view1.heightAnchor constraintEqualToConstant:100].active = true;
        [view1.widthAnchor constraintEqualToConstant:120].active = true;
    
    UIStackView *stackView = [[UIStackView alloc] init];
        stackView.axis = UILayoutConstraintAxisVertical;
        stackView.distribution = UIStackViewDistributionEqualSpacing;
        stackView.alignment = UIStackViewAlignmentCenter;
        stackView.spacing = 30;
    
    
        [stackView addArrangedSubview:Imageview1];
        [stackView addArrangedSubview:Imageview2];
        [stackView addArrangedSubview:Imageview3];
    
        stackView.translatesAutoresizingMaskIntoConstraints = false;
        [self.view addSubview:stackView];
    
    
        //Layout for Stack View
        [stackView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = true;
        [stackView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = true;
    

    【讨论】:

    • 如果我的图像堆栈视图占用的空间比屏幕上的空间多,这是否会调整它们的大小以适应或只是将它们切断(我希望它们被切断)?
    • 让它更简单......添加collectionview并自定义仅包含图像视图的UIcollectionviewcell......并添加行数等于图像视图数。由于集合视图是滚动视图的子视图,因此您不必担心滚动........在这种情况下,您必须在滚动视图中添加堆栈视图......
    • 为什么比使用 UITableView 更简单?
    • 您可以自定义集合视图以水平和垂直滚动......将来如果您需要水平滚动,您必须使用集合视图......除非 UITableview 和 UICollectionview是一样的......我使用collectionview而不是UItableview bcs在UIcollectionview中定制很容易。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多