Resizing UIButton when using Autolayout?

I am very new to ios. I am working on a project that will use a specific UIView class frequently throughout my application. This class is just an image that makes some transparency elements with the background color. So far, that's fine. This class sometimes exists as a single UIView, and sometimes as a subname for buttons.

When I add this UIView to the UIButton as a sub-object, the full content of the UIView is displayed at full size, but the area when the button is clicked remains the size specified in the xib, unless I disable the use of Autolayout (even if I manually try to set the border of the button.)

I would like to put a UIButton in xib as a placeholder and then determine its size / click area based on the size of the overlay image that the UIView has initialized.

If this is possible, or have I misinterpreted the use of xib files / autorun?

under ViewDidLoad I have ...

- (void)viewDidLoad{

    [theButton addSubview:_theView];
    CGRect buttonFrame = theButton.frame;
    buttonFrame.size = CGSizeMake(_theView.getSize.width,_theView.getSize.height);
    [theButton setFrame:buttonFrame];
}

      

However, the frame remains the same size when I print the button information before and after trying to call setFrame.

(note that '_theView.getSize' was added by me)

0


source to share


1 answer


  • In the Autolayout section, views have no frames in viewDidLoad

    . Try the code in viewWillAppear:

    or viewDidLayoutSubviews

    .
  • In the Autolayout section, you don't set frames. Instead, you edit the constraints. The frame setup will work until the next layout passes, when your layout reverts to describing your constraints.
  • To get the size of the button to fit under your watch, you can try something like this (in visual format language): |-[theView]-|

    but it will depend on what restrictions exist on your xib.


+6


source







All Articles