Swift&AutoLayout

来源:互联网 发布:mac版bt下载软件 编辑:程序博客网 时间:2024/06/07 05:26
let logoView = UIImageView(image: UIImage(named: "logo"))logoView.translatesAutoresizingMaskIntoConstraints = false//这句话一定要设置,否则自动布局无效,而且需要设置在需要设置布局的控件上,而不是controller的Viewview.addSubview(logoView)//布局原理即是:A(控件A)=B(控件B)*X(一定比例系数)+C(常量)view.addConstraint(NSLayoutConstraint(item: logoView,attribute: .top,relatedBy: .equal,toItem:playerView,attribute: .top,multiplier: 1,constant: 30))//上述代码的意思是:logoView.top = playerView.top * 1 + 30//此处约束是添加在父控件身上view.addConstraint(NSLayoutConstraint(item: logoView,attribute: .centerX,relatedBy: .equal,toItem: playerView,attribute: .centerX,multiplier: 1,constant: 0))//此处约束是添加在自己身上logoView.addConstraint(NSLayoutConstraint(item:logoView,attribute: .width,relatedBy: .equal,                             toItem: nil,//如果约束和其他控件无关,此处传nil                                   attribute: .notAnAttribute, //此处传notAnAttribute multiplier: 0,//此处为0,因为没有作为参照的控件                                 constant: 100));logoView.addConstraint(NSLayoutConstraint(item:logoView,attribute: .height,relatedBy: .equal,toItem: nil,attribute: .notAnAttribute,multiplier: 0,constant: 100));
0 0
原创粉丝点击