简体   繁体   中英

how to redraw custom view from the controller in ios objective-c

I have a custom view with 3 buttons taking full width.

I import the class to my controller where I want to hide on button (menuBtn) and make one of the one of the other buttons (searchbarBtn) be bigger to fill the empty space by doing:

`self.topMenuView.searchbarBtn.frame = CGRectMake(self.topMenuView.searchbarBtn.frame.origin.x, self.topMenuView.searchbarBtn.frame.origin.x, self.topMenuView.searchbarBtn.frame.size.width + self.topMenuView.menuBtn.frame.size.width , self.topMenuView.searchbarBtn.frame.size.height);`

I do this in viewWillLayoutSubviews (have also tried in viewWillAppear ) and I call the

[self.topMenuView.searchbarBtn setNeedsDisplay];

but nothings happens.

I

The button's layout properties override your frame settings, since by definition viewWillLayoutSubvies is called before the layout pass. You should just set

self.topMenuView.menuBtn.hidden = YES;

and use autolayout constraints (in the interface builder or in the code) between menuBtn , searchbarBtn and topMenuView to make sure your buttons grow as needed.

As a general rule, we should strive to create interface so that views will correctly position themselves provided their inner state is correctly set and correct constraints are formed, without explicit corrections on our part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM