Centering a view in its superview using Visual Format Language

前端 未结 14 1361
天命终不由人
天命终不由人 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:49

    Absolutely possible was able to do it like this:

    [NSLayoutConstraint constraintsWithVisualFormat:@"H:[view]-(<=1)-[subview]"
                                            options:NSLayoutFormatAlignAllCenterY
                                            metrics:nil
                                              views:NSDictionaryOfVariableBindings(view, subview)];
    

    Learned this from here. Code above centers subview in view by Y obviously.

    Swift3:

            NSLayoutConstraint.constraints(withVisualFormat: "H:[view]-(<=1)-[subview]",
                                           options: .alignAllCenterY,
                                                           metrics: nil,
                                                           views: ["view":self, "subview":_spinnerView])
    

提交回复
热议问题