ios - 如何以编程方式制作此自动布局?

标签 ios xcode swift

如何为所有设备制作标签自动布局。

        golabel = UILabel(frame: CGRectMake(30, 100, 350, 100))
        golabel.text = "Game Over"
        golabel.textColor = UIColor.whiteColor()
        golabel.font = UIFont(name: "AppleSDGothicNeo-Thin" , size: 70)
        self.view.addSubview(golabel)

最佳答案

let golabel = UILabel()

golabel.setTranslatesAutoresizingMaskIntoConstraints(false)

let horizontalConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 30)
view.addConstraint(horizontalConstraint)

let verticalConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 100)
view.addConstraint(verticalConstraint)

let widthConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 350)
golabel.addConstraint(widthConstraint)

let heightConstraint = NSLayoutConstraint(item: golabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
golabel.addConstraint(heightConstraint)

关于ios - 如何以编程方式制作此自动布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30931697/

相关文章:

ios - 从 Objective-c 快速进行 block 转换

iphone - 如何在Xcode中模拟iOS应用程序崩溃?

iphone - MFMailComposeViewController 模型不显示

ios - Firebase crashlytics 报告我的应用程序中存在许多可用的崩溃见解

ios - 以编程方式设置 UITextView 字体大小

ios - 健康应用程序在一段时间后忽略步骤

ios - 自定义按钮view : detect touch down/up, 快速回退拖拽到UIScrollView

ios - 为什么 iOS 上的 Codename One 将音频录制为 CAF?

xcode - 如何将Table View中的导航栏设置到顶部?

ios - 如何以编程方式快速更改语言环境和 Storyboard?