Cannot change UIView frame size

12,959

Solution 1

I guess you have hooked self.view to UIView in Interface Builder.

In order to change UIView frame, in Interface Builder go to File Inspector, and uncheck Use Autolayout, then in the code change the frame.

Hope this helps.

Solution 2

You need to set adjust the frame in the viewDidAppear (not viewDidLoad).

- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

CGRect frame = self.tableView.frame;
frame.origin.y += 100.0;
frame.size.height -= 100.0;
self.tableView.frame = frame;
}

Apparently this has something to do with table views within navigation controllers.

Solution 3

It is because of Auto Layout, however you could do it after Auto Layout done its work or change constraints of it. If you want to do it after auto layout add following.

- (void)viewDidAppear:(BOOL)animated {
    dispatch_async(dispatch_get_main_queue(), ^{
        box.frame = CGRectMake(20, 20, 100, 73)];
});
Share:
12,959
michaela
Author by

michaela

Updated on June 07, 2022

Comments

  • michaela
    michaela about 2 years

    I have a custom uiview that i am trying to update the frame size. I do it like this in a method once it is clicked. The method is called, and the frame value changes, however on the screen nothing happens!

    if (!touched) {
    
            GameBox *box = (GameBox *)[self.view viewWithTag:40];
    
            [box setFrame:CGRectMake(20, 20, 100, 73)];
            NSLog(@"%f", box.frame.origin.x);
            markButton.enabled = NO;
            guessButton.enabled = NO;
            [self.view reloadInputViews];
    
            NSLog(@"The action bar should now be hidden");
        }
        else if (touched) {
            guessButton.enabled = YES;
            markButton.enabled = YES;
            [self.view reloadInputViews];
            NSLog(@"The action bar should now be visible");
        }
    
    • Nitin
      Nitin about 12 years
      Please ask what you want to do..so I can understand..
    • michaela
      michaela about 12 years
      I want to: a) resize the cgrect frame of the uisubview box, and b) disable/reenable the guess button and mark button
  • OMGPOP
    OMGPOP over 10 years
    I can't believe I spent 3 hours debugging...Thank you!
  • Chatar Veer Suthar
    Chatar Veer Suthar almost 10 years
    It helped me, Thanks, but can you tell me reason why you put it inside dispatch block @Deniz
  • Deniz
    Deniz almost 10 years
    It seems that I had wanted frame size to be changed after current repaint cycle, some time after viewDidAppear method finished executing. But I am not sure now.
  • Max Friedman
    Max Friedman over 9 years
    This works but it affects the ability to use autolayout for the whole document...
  • saintjab
    saintjab almost 8 years
    You can simply set translatesAutoresizingMaskIntoConstraints = YES; on the uiView element and still keep your autolayout. This will redraw the element to new frame.