Why is the UIView frame not updated in ViewDidLayoutSubviews?

I am trying to update a UIView frame that contains buttons and labels inside. I am trying to update it in viewDidLayoutSubviews (and I also tried in viewDidLoad , viewWillAppear , viewDidAppear ..). I want to change the y position (origin.y) of the view. NSLogs says that my initial position is y is 334, and after the change - 100. However, the position does not change in my view. I have already verified that the view is connected in the storyboard. What am I doing wrong?

 -(void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; CGRect theFrame = [self.bottomView frame]; NSLog(@"Y position bottomview: %f", self.bottomView.frame.origin.y); if([[UIScreen mainScreen] bounds].size.height == 568) //iPhone 4inch { // NSLog(@"iphone5"); } else{ // NSLog(@"iphone4"); theFrame .origin.y = 100; } self.bottomView.frame = theFrame; NSLog(@"Y position bottomview after changing it: %f", self.bottomView.frame.origin.y); [self.view layoutIfNeeded]; } 
+6
ios objective-c uiview runtime frame
Mar 18 '14 at 19:05
source share
3 answers

The problem is with Autolayout. However, I could not disable it, since I use autostart in my project. I decided to define the appropriate restrictions in the view. Then there is no need to check whether it is an iPhone 4inch or 3.5inch, and change the position of the frame, as it automatically adapts to each size.

+3
Apr 03 '14 at 16:53 on
source share

I had the same problem. Forcing a layout for your viewview helped me:

 -(void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self.bottomView.superview setNeedsLayout]; [self.bottomView.superview layoutIfNeeded]; // Now modify bottomView frame here } 

In Swift:

 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() bottomView.superview!.setNeedsLayout() bottomView.superview!.layoutIfNeeded() // Now modify bottomView frame here } 
+10
Dec 27 '15 at 12:40
source share

Install AutoLayout NO from your xib file or storyboard.

+2
Mar 24 '14 at 16:24
source share



All Articles