Center UIButton programmatically

前端 未结 2 2034
无人及你
无人及你 2020-12-18 12:22

I have a UIButton which i add programmatically. No problems here, it stetches as it should and looks good. However, when I tilt the phone to landscape the button gets out of

相关标签:
2条回答
  • 2020-12-18 12:49

    One thing before I answer your question:

    [_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    [_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
    

    The second sentence overwrites the first one, you should use the | operator so both resizing mask are applied:

    [_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
    

    To keep the element where it is when the view changes its size, you need to tell it not to use any resizing mask:

    [_notMeButton setAutoresizingMask:UIViewAutoresizingNone];
    

    And the equivalent in Interface Builder is this:

    IB autoresizing mask applier

    I hope it helps

    0 讨论(0)
  • 2020-12-18 12:56

    Your idea about autoresizing mask is right, but you're setting it incorrectly:

    [_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    [_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
    

    The second line overwrites what you set in the 1st. Correct way to set several flags will be:

    [_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
    
    0 讨论(0)
提交回复
热议问题