28

I have an UIButton placed with constraints. In the interface of the view controller I created an IBOutlet for the height constraint of the button. In viewDidLoad method I have the code:

myButtonConstraint.constat = 0;
[self.view layoutIfNeeded];

In viewWillAppear the height of the button is 0, but on the simulator the title of the button is still visible. Even though the title of the button is visible, the button is not tappable, it performs no action.

What is going on?

Andrei Marcu
  • 588
  • 6
  • 9

3 Answers3

63

You can check Clip Subviews on interface builder or if you want do it by code try this:

myButton.clipsToBounds = YES

from apple documentation:

clipsToBounds

A Boolean value that determines whether subviews are confined to the bounds of the view.

@property(nonatomic) BOOL clipsToBounds

Discussion

Setting this value to YES causes subviews to be clipped to the bounds of the receiver. If set to NO, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is NO.

Community
  • 1
  • 1
rafaperez
  • 1,028
  • 12
  • 16
4

Maybe you should call

[button.layer setMasksToBounds:true];

and then layoutIfNeeded.

Obs.: probably requires the Quartz framework.

-5

The title is always visible regardless of the button heigth, it is a UILabel with it's own size setting. Why do you want to have a button with height 0 anyway? If you want to hide a button simply set the Hidden property of your button to true;

[button setHidden:TRUE];
ophychius
  • 2,623
  • 2
  • 27
  • 49
  • Not true, if you set the clipsToBounds property to YES, the title will hide. Also, if you simply hide the button, it will still take up space in the view, except it's just empty. Setting its height to 0 is definitely valid. – Bassem Sameh Feb 10 '16 at 10:59