[英]how to redraw custom view from the controller in ios objective-c
I have a custom view with 3 buttons taking full width. 我有一个自定义视图,其中3个按钮占据了整个宽度。
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: 我将类导入控制器中要在其上隐藏的按钮(menuBtn),并通过执行以下操作使其他按钮之一(searchbarBtn)变大以填充空白:
`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 我在
viewWillLayoutSubviews
进行了此操作(也在viewWillAppear
尝试),我将
[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. 该按钮的布局属性会覆盖您的框架设置,因为根据定义,
viewWillLayoutSubvies
在布局传递之前被调用。 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. 并使用自动布局的限制(在界面生成器或代码)之间
menuBtn
, searchbarBtn
和topMenuView
,以确保您的按钮成长需要。
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. 通常,我们应该努力创建接口,以便在正确设置视图的内部状态并形成正确约束的情况下,视图可以正确地定位自己,而无需我们进行任何明确的更正。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.