我有一个XIB,该XIB定义了一个单个灰色矩形的视图。该视图以编程方式添加到UisCrollView中的UistackView。添加的视图恰好显示出来,直到我尝试在灰色矩形上添加任何约束,然后它们都消失了。我没有经验丰富的约束方式,但是我尝试添加任何约束都会导致灰色矩形消失。我是iOS开发的新手,因此任何朝着正确方向的指针都赞赏。
也许就是您设置约束的方式(也许您可以发布代码?)。我更喜欢用接口构建器设置约束,因为我是iOS开发的新手。这个答案可能会对您有所帮助。
您打算拥有宽度的平方
UIView
:100 和高度: 100 以UIViewController
的UIView
为中心?如果是这样,你 可以尝试以下7个Swift 3自动布局片段之一:
1。
NSLayoutConstraint
addConstraints(_ constraints: [NSLayoutConstraint])
样式override func viewDidLoad() { super.viewDidLoad() let newView = UIView() newView.backgroundColor = UIColor.red newView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(newView) let horizontalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.centerX, relatedBy:
nslayoutrelation.equal,toitem:查看,属性: nslayoutattribute.centerx,乘数:1,常数:0) 令verticalconstraint = nslayoutconstraint(项目:newView,属性:nslayoutattribute.centery,celtersby: nslayoutrelation.equal,toitem:查看,属性: nslayoutattribute.centery,乘数:1,常数:0) 令widthconstraint = nslayoutconstraint(项目:newview,属性:nslayoutattribute.width,celtersby:nslayoutrelation.equal,equal,equal, toitem:nil,属性:nslayoutattribute.notanattribute,乘数: 1,常数:100) LET height constraint = nslayoutconstraint(item:newView,属性:nslayoutattribute.height,Relatesby: nslayoutrelation.equal,toitem:nil,属性: nslayoutattribute.notanattribute,乘数:1,常数:100)
view.addConstraints([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint]) }
2。
NSLayoutConstraint
activate(_ constraints: [NSLayoutConstraint])
样式注意:`激活(_约束:[nslayoutconstraint])`要求ios 8
override func viewDidLoad() { super.viewDidLoad() let newView = UIView() newView.backgroundColor = UIColor.red newView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(newView) let horizontalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.centerX, relatedBy:
nslayoutrelation.equal,toitem:查看,属性: nslayoutattribute.centerx,乘数:1,常数:0) 令verticalconstraint = nslayoutconstraint(项目:newView,属性:nslayoutattribute.centery,celtersby: nslayoutrelation.equal,toitem:查看,属性: nslayoutattribute.centery,乘数:1,常数:0) 令widthconstraint = nslayoutconstraint(项目:newview,属性:nslayoutattribute.width,celtersby:nslayoutrelation.equal,equal,equal, toitem:nil,属性:nslayoutattribute.notanattribute,乘数: 1,常数:100) LET height constraint = nslayoutconstraint(item:newView,属性:nslayoutattribute.height,Relatesby: nslayoutrelation.equal,toitem:nil,属性: nslayoutattribute.notanattribute,乘数:1,常数:100)
NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint]) }
3。纯视觉格式语言样式
addConstraints(_ constraints: [NSLayoutConstraint])
样式override func viewDidLoad() { super.viewDidLoad() let newView = UIView() newView.backgroundColor = UIColor.red newView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(newView) let views = ["view": view, "newView": newView] let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat:
" h:[view] - (< = 0) - [newview(100)]",选项: nslayoutformatoptions.AlignallCentery,指标:nil,视图:视图) 令verticalconstraints = nslayoutconstraint.constraints(withvisualformat: " v:[view] - (< = 0) - [newview(100)]",选项: nslayoutformatoptions.alignallcenterx,指标:nil,视图:视图)
view.addConstraints(horizontalConstraints) view.addConstraints(verticalConstraints) }
4。
NSLayoutConstraint
视觉格式语言activate(_ constraints: [NSLayoutConstraint])
样式注意:`activate(_约束:[nslayoutconstraint])`要求ios 8
override func viewDidLoad() { super.viewDidLoad() let newView = UIView() newView.backgroundColor = UIColor.red newView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(newView) let horizontalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.centerX, relatedBy:
nslayoutrelation.equal,toitem:查看,属性: nslayoutattribute.centerx,乘数:1,常数:0) 令verticalconstraint = nslayoutconstraint(项目:newView,属性:nslayoutattribute.centery,celtersby: nslayoutrelation.equal,toitem:查看,属性: nslayoutattribute.centery,乘数:1,常数:0)
let views = ["newView": newView] let widthConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:[newView(100)]",
选项:nslayoutformatoptions(RawValue:0),指标:nil,视图: 景色) LET height constraints = nslayoutconstraint.constraints(withvisualformat:" v:[newView(100)]", 选项:nslayoutformatoptions(RawValue:0),指标:nil,视图: 视图)
NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint]) NSLayoutConstraint.activate(widthConstraints) NSLayoutConstraint.activate(heightConstraints) }
5。
UIViewAutoresizing
样式注意:弹簧和支柱将在运行时转换为相应的自动布局约束。override func viewDidLoad() { super.viewDidLoad() let newView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) newView.backgroundColor = UIColor.red newView.translatesAutoresizingMaskIntoConstraints = true view.addSubview(newView) newView.center = CGPoint(x: view.bounds.midX, y: view.bounds.midY) newView.autoresizingMask = [UIViewAutoresizing.flexibleLeftMargin,
uiviewAutoresizing.flexiblerightmargin, uiviewautoresizing.flexibletopmargin, uiviewAutoresizing.flexibleBottommargin] }
6。
NSLayoutAnchor
activate(_ constraints: [NSLayoutConstraint])
样式注意:`激活(_约束:[nslayoutconstraint])
requires iOS 8,
nslayoutanchor`要求ios 9override func viewDidLoad() { super.viewDidLoad() let newView = UIView() newView.backgroundColor = UIColor.red newView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(newView) let horizontalConstraint = newView.centerXAnchor.constraint(equalTo: view.centerXAnchor) let verticalConstraint = newView.centerYAnchor.constraint(equalTo: view.centerYAnchor) let widthConstraint = newView.widthAnchor.constraint(equalToConstant: 100) let heightConstraint = newView.heightAnchor.constraint(equalToConstant: 100) NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint]) }
7。子类
activate(_ constraints: [NSLayoutConstraint])
样式注意:activate(_ constraints: [NSLayoutConstraint])
需要iOS 8,
NSLayoutAnchor
需要iOS 9import UIKit class CustomView: UIView { override var intrinsicContentSize: CGSize { return CGSize(width: 100, height: 100) } } class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let newView = CustomView() newView.backgroundColor = UIColor.red newView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(newView) let horizontalConstraint = newView.centerXAnchor.constraint(equalTo: view.centerXAnchor) let verticalConstraint = newView.centerYAnchor.constraint(equalTo: view.centerYAnchor) NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint]) } }
另外,检查文档