我有一个xib定义了一个单个灰色矩形的视图。视图以编程方式添加到UIStackView中,该视图位于UIScrollView中。添加的视图显示正常,直到我尝试向灰色矩形添加任何约束,然后它们都消失。 我对约束的工作方式没有经验,但是我尝试添加的任何约束都会导致灰色矩形消失。 我不熟悉iOS开发,所以任何指向正确方向的人都会赞赏。
答案 0 :(得分:-1)
也许只是你设置约束的方式(也许你可以发布你的代码?)。我更喜欢使用界面构建器设置约束,因为我是iOS开发的新手。 This answer可能会对您有所帮助。
您是否打算平方
UIView
宽度:100 和高度: 100 位于UIView
的{{1}}内?如果是的话,你 可以尝试以下7个Swift 3 Auto Layout片段中的一个:1。
UIViewController
+NSLayoutConstraint
样式addConstraints(_ constraints: [NSLayoutConstraint])
NSLayoutRelation.equal,toItem:view,attribute: NSLayoutAttribute.centerX,乘数:1,常数:0) let verticalConstraint = NSLayoutConstraint(item:newView,attribute:NSLayoutAttribute.centerY,relatedBy: NSLayoutRelation.equal,toItem:view,attribute: NSLayoutAttribute.centerY,乘数:1,常数:0) let widthConstraint = NSLayoutConstraint(item:newView,attribute:NSLayoutAttribute.width,relatedBy:NSLayoutRelation.equal, toItem:nil,attribute:NSLayoutAttribute.notAnAttribute,multiplier: 1,常数:100) let heightConstraint = NSLayoutConstraint(item:newView,attribute:NSLayoutAttribute.height,relatedBy: NSLayoutRelation.equal,toItem:nil,attribute: NSLayoutAttribute.notAnAttribute,乘数:1,常数:100)
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:
2。
view.addConstraints([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint]) }
+NSLayoutConstraint
样式注意:`activate(_ constraints:[NSLayoutConstraint])`需要iOS 8
activate(_ constraints: [NSLayoutConstraint])
NSLayoutRelation.equal,toItem:view,attribute: NSLayoutAttribute.centerX,乘数:1,常数:0) let verticalConstraint = NSLayoutConstraint(item:newView,attribute:NSLayoutAttribute.centerY,relatedBy: NSLayoutRelation.equal,toItem:view,attribute: NSLayoutAttribute.centerY,乘数:1,常数:0) let widthConstraint = NSLayoutConstraint(item:newView,attribute:NSLayoutAttribute.width,relatedBy:NSLayoutRelation.equal, toItem:nil,attribute:NSLayoutAttribute.notAnAttribute,multiplier: 1,常数:100) let heightConstraint = NSLayoutConstraint(item:newView,attribute:NSLayoutAttribute.height,relatedBy: NSLayoutRelation.equal,toItem:nil,attribute: NSLayoutAttribute.notAnAttribute,乘数:1,常数:100)
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:
3。纯视觉格式语言风格+
NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint]) }
样式addConstraints(_ constraints: [NSLayoutConstraint])
“H:[view] - (< = 0) - [newView(100)]”,选项: NSLayoutFormatOptions.alignAllCenterY,metrics:nil,views:views) let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: “V:[view] - (< = 0) - [newView(100)]”,选项: NSLayoutFormatOptions.alignAllCenterX,metrics:nil,views:views)
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:
4。
view.addConstraints(horizontalConstraints) view.addConstraints(verticalConstraints) }
+可视格式语言+NSLayoutConstraint
样式注意:`activate(_约束:[NSLayoutConstraint])`需要iOS 8
activate(_ constraints: [NSLayoutConstraint])
NSLayoutRelation.equal,toItem:view,attribute: NSLayoutAttribute.centerX,乘数:1,常数:0) let verticalConstraint = NSLayoutConstraint(item:newView,attribute:NSLayoutAttribute.centerY,relatedBy: NSLayoutRelation.equal,toItem:view,attribute: NSLayoutAttribute.centerY,乘数:1,常数:0)
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:
选项:NSLayoutFormatOptions(rawValue:0),metrics:nil,views: 观点) let heightConstraints = NSLayoutConstraint.constraints(withVisualFormat:“V:[newView(100)]”, options:NSLayoutFormatOptions(rawValue:0),metrics:nil,views: 视图)
let views = ["newView": newView] let widthConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:[newView(100)]",
5。
NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint]) NSLayoutConstraint.activate(widthConstraints) NSLayoutConstraint.activate(heightConstraints) }
样式注意:Springs和Struts将在运行时转换为相应的自动布局约束。UIViewAutoresizing
UIViewAutoresizing.flexibleRightMargin, UIViewAutoresizing.flexibleTopMargin, UIViewAutoresizing.flexibleBottomMargin] }
6。
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,
+NSLayoutAnchor
样式注意:`activate(_ constraints:[NSLayoutConstraint])
activate(_ constraints: [NSLayoutConstraint])
NSLayoutAnchor`需要iOS 9requires iOS 8,
7。子类化+
override 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]) }
样式注意:activate(_ constraints: [NSLayoutConstraint])
需要iOS 8,
activate(_ constraints: [NSLayoutConstraint])
需要iOS 9NSLayoutAnchor