【问题标题】:IOS/Objective-C/Autolayout Center four images horizontallyIOS/Objective-C/Autolayout 水平居中四张图片
【发布时间】:2018-02-23 01:07:22
【问题描述】:

我很容易使用 autplayout 将三个图像居中。我将中间一个水平居中,将所有三个设置为相同的基线,然后设置外部相对于中间一个的前导和尾随空格。

但是,现在我需要添加第四张图像,因此将偶数张图像居中。所以我不能再用同样的伎俩了。我想避免添加更多子视图,但想不出一个简单的方法来做到这一点。

任何人都可以推荐一种将偶数水平图像水平居中的首选做法吗?

这是三张图片的样子。我想添加第四个。

提前感谢您的任何建议。

【问题讨论】:

  • 你看过UIStackView吗?

标签: ios autolayout


【解决方案1】:

我建议您使用堆栈视图。对于您的情况,您可以使用水平堆栈视图,并将分布设置为平均填充。根据您的要求为您的堆栈视图设置约束,并将您的图像视图放在堆栈视图中。当您想要添加新图像时,您只需拖动它,它就会相应地进行调整。

看起来像这样

如果你添加另一个,那么它会是这样的。

您可以在此处阅读有关如何使用堆栈视图的更多信息

Apple Documentation

Good tutorial to follow

【讨论】:

    【解决方案2】:

    使用UIStackView 处理此类工作:

    let stackView = UIStackView()
    
    stackView.distribution = .fill
    stackView.alignment = .center
    stackView.axis = .horizontal
    stackView.spacing = 30 // or whatever you need
    
    // now add the dots to your stackView:
    stackView.addArrangedSubview(dot1)
    stackView.addArrangedSubview(dot2)
    stackView.addArrangedSubview(dot3)
    stackView.addArrangedSubview(dot4)
    
    // and just add and lay out the stackView, stackView itself will take care of positioning the dots
    self.view.addSubview(stackView)
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2012-09-29
      • 1970-01-01
      相关资源
      最近更新 更多