【问题标题】:Why add UIView in contentView center always not in center?为什么在 contentView 中心添加 UIView 总是不在中心?
【发布时间】:2016-12-13 04:01:51
【问题描述】:

我想在它的 contentView 中心添加一个 UIView。

    @IBOutlet weak var contentView: UIView!
override func viewDidLoad() {
    let view = UIView(frame: CGRectMake(0,0,100,100))
    view.backgroundColor = UIColor.blueColor()
    contentView.backgroundColor = UIColor.redColor()
    contentView.addSubview(view)
    view.center = contentView.center

}

但结果是

我是不是忘记了什么?

更新 感谢@Wain 的提示,约束工作。 根据https://stackoverflow.com/a/27624927/6006588

    override func viewDidLoad() {
    let view = UIView(frame: CGRectMake(0,0,100,100))
    view.backgroundColor = UIColor.blueColor()
    contentView.backgroundColor = UIColor.redColor()
    view.translatesAutoresizingMaskIntoConstraints = false
    contentView.addSubview(view)
    let widthConstraint = NSLayoutConstraint(item: view, attribute: .Width, relatedBy: .Equal,
                                             toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100)
    let heightConstraint = NSLayoutConstraint(item: view, attribute: .Height, relatedBy: .Equal,
                                              toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100)
     let xConstraint = NSLayoutConstraint(item: view, attribute: .CenterX, relatedBy: .Equal, toItem: contentView, attribute: .CenterX, multiplier: 1, constant: 0)
    let yConstraint = NSLayoutConstraint(item: view, attribute: .CenterY, relatedBy: .Equal, toItem: contentView, attribute: .CenterY, multiplier: 1, constant: 0)
    NSLayoutConstraint.activateConstraints([heightConstraint,  widthConstraint,xConstraint, yConstraint])
}

我想我在 StoryBoard 中对 contentView 使用了约束(centerX、centerY、Equal Width、Equal Height 到 superView)。
ContentView 在 ViewDidLoad 中的帧大小不正确。
当我设置 view.center = contentView.center 它不起作用。
我需要以编程方式使用约束来设置视图的位置。
谢谢。

【问题讨论】:

  • Yout 代码在 6S 模拟器中对我来说运行良好,但在 6S+ 中却不行。我是如何通过代码实现的。它适用于所有模拟器
  • 你为什么不使用约束?
  • @Wain 。原因是我需要在滚动视图中添加从服务器接收的动态视图。我需要在每个视图的中心添加按钮,比如 Youtube 播放按钮。你有什么好主意吗?
  • 是的,约束...试试看

标签: ios swift


【解决方案1】:

试试这个 -

let view = UIView(frame: CGRectMake((contentView.frame.size.width-100)/2,(contentView.frame.size.height-100)/2,100,100))
    view.backgroundColor = UIColor.blueColor()
    contentView.backgroundColor = UIColor.redColor()
    contentView.addSubview(view)

【讨论】:

    【解决方案2】:

    你可以使用这个代码

    @IBOutlet weak var contentView: UIView!
    

    然后将其添加到 viewDidLoad

    let view = UIView(frame: CGRectMake(0,0,100,100))
    let contentView = UIView(frame: self.view.frame)
    view.backgroundColor = UIColor.blueColor()
    contentView.backgroundColor = UIColor.redColor()
    view.center = contentView.center
    contentView.addSubview(view)
    self.view.addSubview(contentView)
    

    这是结果

    【讨论】:

    • 谢谢。这行得通。但是为什么要重置 contentView 框架?我更喜欢在 StoryBoard 中设置约束。约束让视图在 viewDidLoad 中的大小不正确吗?
    猜你喜欢
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2012-10-13
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2013-06-08
    相关资源
    最近更新 更多