以编程方式创建绑定到视图控制器边距的约束 [英] Programmatically creating constraints bound to view controller margins

查看:64
本文介绍了以编程方式创建绑定到视图控制器边距的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个视图,该视图将充当一种面板",附加到视图控制器的右侧.

I'm trying to make a view that will act as a sort of "panel", attached to the right side of the view controller.

即绑定到父视图控制器的尾部、上、下边距,静态宽度为300

That is, it is bound to the trailing, top, and bottom margins of the parent view controller, with a static width of 300

然而,我似乎无法做到正确,我要么打破约束,要么做一些 xcode 告诉我是非法的事情.

However, I just can't seem to get it right, I'm either breaking a constraint or doing something xcode tells me is illegal.

我做错了什么?

这是控制器中的代码

    let myView = UIView()
    view.backgroundColor = UIColor.redColor()
    self.view.addSubview(view)
    let topConstraint = NSLayoutConstraint(item: myView,
                                           attribute: .Top,
                                           relatedBy: .Equal,
                                           toItem: self.topLayoutGuide,
                                           attribute: .Bottom,
                                           multiplier: 1,
                                           constant: 0)

    let trailingConstraint = NSLayoutConstraint(item: self.view,
                                                attribute: .TrailingMargin,
                                                relatedBy: .Equal,
                                                toItem: myView,
                                                attribute: .Trailing,
                                                multiplier: 1,
                                                constant: 0)

    let bottomConstraint = NSLayoutConstraint(item: self.bottomLayoutGuide,
                                              attribute: .Top,
                                              relatedBy: .Equal,
                                              toItem: myView,
                                              attribute: .Bottom,
                                              multiplier: 1,
                                              constant: 0)

    let widthConstraint = NSLayoutConstraint(item: myView,
                                             attribute: .Width,
                                             relatedBy: .Equal,
                                             toItem: nil,
                                             attribute: .NotAnAttribute,
                                             multiplier: 1,
                                             constant: 300)

    self.view.addConstraints([trailingConstraint])
    view.addConstraints([topConstraint, bottomConstraint, widthConstraint])

推荐答案

其实你代码的问题是你没有把myviewtranslatesAutoresizingMaskIntoConstraints设置为false,每当您想使用自动布局约束时,您都必须将视图的 translatesAutoresizingMaskIntoConstraints 设置为 false.另一个问题是你没有在 self.view 上添加 myview 我已经更新了你的代码并且它根据你的约束工作正常.

Actually the problem in your code is that you did not set the translatesAutoresizingMaskIntoConstraints of myview to false, whenever you want to use auto-layout constraints then you have to set translatesAutoresizingMaskIntoConstraints of a view to false. Another Problem is that you do not add myview on self.view I have updated your code and Its working fine According your constraints.

将下面的代码放在你的 ViewController 中.

Put below code in your ViewController .

let myView = UIView()
myView.backgroundColor = UIColor.redColor()
self.view.addSubview(myView)
myView.translatesAutoresizingMaskIntoConstraints = false

view.addConstraint(NSLayoutConstraint(item: myView, attribute: .Top, relatedBy: .Equal, toItem: self.topLayoutGuide, attribute: .Bottom, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: myView, attribute: .Bottom, relatedBy: .Equal, toItem: self.bottomLayoutGuide, attribute:.Top, multiplier: 1, constant: 20))

view.addConstraint(NSLayoutConstraint(item: myView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute,multiplier: 1, constant: 300))
view.addConstraint(NSLayoutConstraint(item: myView, attribute: .TrailingMargin, relatedBy: .Equal, toItem: view, attribute: .TrailingMargin, multiplier: 1, constant: 0))

这篇关于以编程方式创建绑定到视图控制器边距的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆