Swift NSLayoutConstraint Equation Based Constant

放肆的年华 提交于 2019-12-23 05:29:35

问题


I have been learning Swift programmatically, and in doing so building a camera (film) app.

I am working on constraints, and it is pretty much straight forward. But what I would like to do now is implement aspect ratios; the user can cycle through standard film and TV aspect ratios that will determine the size of the view.

As I am building a universal app, the constrains need to be based on the height of the view. So, I need a constant that is an equation and not a numerical value.

Something like:

constant:self.view.Height/((self.view.Width*Consequent)/Antecedent)

I have found the below blog post, and have been basing my attempt off of it. But as the post was written when Swift was in BETA, I am having a hard time recreating anything.

http://blog.ikiapps.com/post/91024428050/making-auto-layout-constraints-more-like-math-in

I am guessing the solution is pretty simple, but as I am new to Swift (and iOS development) I am probably overlooking something. Any help, advice, known posts, etc. would be much appreciated! Thanks!


回答1:


Set Constraints Programmatically

    var verticalSpaceFromBotton = NSLayoutConstraint(item: objectName, attribute: NSLayoutAttribute.BottomMargin, relatedBy: NSLayoutRelation.Equal, toItem: self.viewControllerName, attribute: .BottomMargin, multiplier: 1, constant: -5)
    self.viewControllerName.addConstraint(verticalSpaceFromBotton)
    var trailingSpace = NSLayoutConstraint(item: objectName, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.viewControllerName, attribute: .Trailing, multiplier: 1, constant: -7)
    self.viewControllerName.addConstraint(trailingSpace)
    var width = NSLayoutConstraint(item: objectName, attribute: .Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.size.width - 14)
    self.viewControllerName.addConstraint(width)
    var height = NSLayoutConstraint(item: objectName, attribute: .Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 44)
    self.viewControllerName.addConstraint(height)


来源:https://stackoverflow.com/questions/31202431/swift-nslayoutconstraint-equation-based-constant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!