Change a UIView Frame

眉间皱痕 提交于 2020-01-21 20:10:33

问题


I have set a UIView in my storyboard and make it an outlet.

 @property (nonatomic, weak) IBOutlet UIView *testView;

In the - (void)viewDidLoad method, I want to change its frame like this

CGRect frame = self.testView.frame;
frame.size.height = 2;
self.testView.frame = frame;

But it does not work. Anybody can tell me why?


回答1:


You might enabled the autolayout in your project , if so the setFrame straightly won't work . Check here for more info

Solution :

  • Disable the autolayout, if you don't need any more
  • Check the above link using with the autolayout .



回答2:


Look at the tiny circle to the left of the property. Is it filled in or hollow? If it's hollow, then it means that you have not connected this particular element of the storyboard to the file's owner.

If the circle is filled in, than the outlet IS connected to something on your storyboard. Just make sure it's connected to the right entity, namely the UIViewController (or subclass thereof) that you are declaring testView within.




回答3:


Assuming its connected to your XIB do the following after changing the view -

[self.view setNeedsDisplay];



回答4:


Trying changing the height to high number and see if that make a difference. I have created demo project for changing and reverting the height. Hope this helps.

https://github.com/rshankras/StackOverflowUIView



来源:https://stackoverflow.com/questions/22522949/change-a-uiview-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!