简体   繁体   English

如何从iOS Objective-C中的控制器重绘自定义视图

[英]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. 并使用自动布局的限制(在界面生成器或代码)之间menuBtnsearchbarBtntopMenuView ,以确保您的按钮成长需要。

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.

相关问题 ios从第二个视图控制器向第一个视图控制器发送Objective-c数据发送 - ios Delegate Objective-c data sending from 2nd view controller to 1st view controller iOS / Objective-C:在SLComposer消息后关闭视图控制器 - IOS/Objective-C: Dismiss view controller after SLComposer message iOS MMDrawerController Objective-C登录视图控制器 - iOS MMDrawerController objective-c login view controller iOS / objective-c:将对象传递到新的视图控制器 - iOS/objective-c: Pass object to new view controller iOS / Objective-C:在展示视图控制器中设置属性 - IOS/Objective-C: Set property in presenting view controller ios / objective-c:结合自定义单元的搜索显示控制器 - ios/objective-c: Search Display Controller combined with custom cell iOS / Objective-C:从共享实例更改View Controller中的属性? - IOS/Objective-C: Change property in View Controller from shared instance? 我需要从SpriteKit GameViewController过渡到iOS应用中的常规View Controller(Objective-C) - I need to transition from a SpriteKit GameViewController to a regular View Controller in an iOS app (Objective-C) Objective-c,向父视图控制器添加自定义UIView - Objective-c, adding a custom UIView to Parent View Controller ios / objective-c:从Textview创建Segue到Storyboard中的View Controller - ios/objective-c: Create Segue from Textview to View Controller in Storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM