基于Swift NSLayoutConstraint方程的常量 [英] Swift NSLayoutConstraint Equation Based Constant

查看:57
本文介绍了基于Swift NSLayoutConstraint方程的常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在以编程方式学习Swift,并以此来构建相机(电影)应用程序.

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.

类似的东西:

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

我找到了以下博客文章,并以此为依据.但是,正如该帖子是在Swift进入测试版时写的一样,我很难重新创建任何内容.

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

我猜想解决方案非常简单,但是由于我是Swift(和iOS开发)的新手,所以我可能会忽略一些东西.任何帮助,建议,已知职位等,将不胜感激!谢谢!

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!

推荐答案

以编程方式设置约束

    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)

这篇关于基于Swift NSLayoutConstraint方程的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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