Centering a view in its superview using Visual Format Language

前端 未结 14 1368
天命终不由人
天命终不由人 2020-12-02 03:52

I just started learning AutoLayout for iOS and had a look at Visual Format Language.

It all works fine except for one thing: I just can\'t get a view to center withi

14条回答
  •  感动是毒
    2020-12-02 04:48

    This is my method

    //create a 100*100 rect in the center of superView
    var consts = NSLayoutConstraint.constraints(withVisualFormat: "H:|-space-[myView]-space-|", options: [], metrics:["space":view.bounds.width/2-50], views:["myView":myView])
    
    consts += NSLayoutConstraint.constraints(withVisualFormat: "V:|-space-[myView]-space-|", options: [], metrics: ["space":view.bounds.height/2-50], views: ["myView":myView])
    

提交回复
热议问题