user2006934
user2006934

Reputation: 201

setFrame won't do anything

I have a problem with an app that won't set frames outside -init and -viewWillLayoutSubviews methods. What should happen when one taps the editButton is an animation that will hide the editor view. Nonetheless, nothing happens as I test it. The problem doesn't come from the animation method since the -setFrame method as it - not included in the block - doesn't work neither.

Here is the code :

-(id)init {

    if (self = [super init]) {

        editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editButtonTapped)];
        doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonTapped)];


        editor = [[UIView alloc] init];
        [editor setBackgroundColor:[UIColor yellowColor]];
        editor.clipsToBounds = YES;
        editorIsOpen = YES;        

        portraitRegularModeEditorRect = CGRectMake(15, 59, 738, 100);
        portraitClosedEditorEditorRect = CGRectMake(15, 59, 738, 0);


    }
    return self;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    [[self view] addSubview:editor];
    [[self view] setBackgroundColor:[UIColor blueColor]];

}


-(void)viewDidAppear:(BOOL)animated {

    [self setForRegularMode];

}




-(void)viewWillLayoutSubviews {

    UIInterfaceOrientation io = [[UIApplication sharedApplication] statusBarOrientation];
    if (io == UIInterfaceOrientationPortrait || io == UIInterfaceOrientationPortraitUpsideDown) {

        //portrait
        [editor setFrame:portraitRegularModeEditorRect];


     } else {

        //landscape

     }


}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(void)editButtonTapped {

    [self setForScenarioLinesEditingMode];

}

-(void)doneButtonTapped {

    [self setForRegularMode];

}


-(void)setForRegularMode {

    editingMode = CPRegularMode;    

    if (!editorIsOpen) {

        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationCurveEaseOut animations:^(void){

            [editor setFrame:portraitRegularModeEditorRect];

        } completion:^(BOOL finished) {

            editorIsOpen = YES;

        }];

    }


    [[self navigationItem] setRightBarButtonItems:[[NSArray alloc] initWithObjects:editButton,nil]];

}



-(void)setForScenarioLinesEditingMode {

    editingMode = CPScenarioLinesEditingMode;

    if (editorIsOpen) {

        [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationCurveEaseOut animations:^(void){

            [editor setFrame:portraitClosedEditorEditorRect];

        } completion:^(BOOL finished) {

            editorIsOpen = NO;

        }];

    }

    [[self navigationItem] setRightBarButtonItems:[[NSArray alloc] initWithObjects:doneButton,nil]];


}

If anyone can help, thanks in advance ;)

Upvotes: 0

Views: 5186

Answers (2)

danypata
danypata

Reputation: 10175

I think that the problem in your case is the fact that in -(void)viewWillLayoutSubviews method you set, lets say the default frame of your view, if you try to change the frame in other methods after the setFrame is called on your view, the -(void)viewWillLayoutSubviews will also be called and the frame of the view will be the default one. Try to remove the setFrame from your -(void)viewWillLayoutSubviews.

Upvotes: 1

jrturton
jrturton

Reputation: 119242

Is your view controller set up in storyboards, and are you using Autolayout (which is on by default?) If so, setFrame won't work and you need to edit constraints after creating outlets to them from the storyboard.

Alternatively, you can turn off Autolayout in your storyboard, as shown here.

Upvotes: 1

Related Questions