简体   繁体   中英

UIToolbar setFrame not working

I want to toggle visibility of a toolbar at the top of the screen by sliding it out of the visible area. To that end I store the toolbar's y-coordinate and height on viewDidLoad, and update it accordingly when I want it to fade-in/fade-out.

The relevant code snippet looks something like this (you can also find a sample project at https://github.com/Duffycola/test/tree/master/TestHideToolbar ):

@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIToolbar* toolbar;
@property (nonatomic, assign) BOOL toolbarVisible;
@property (nonatomic, assign) float toolbarOriginY;
@end

@implementation ViewController

- (void)viewDidLoad;
{
    [super viewDidLoad];

    self.toolbarVisible = YES;
    self.toolbarOriginY = self.toolbar.frame.origin.y;
}

- (IBAction)handleToggleToolbar:(id)sender;
{
    self.toolbarVisible = !self.toolbarVisible;

    [UIView animateWithDuration:0.5 animations:^{

        CGRect toolbarFrame = self.toolbar.frame;

        if (self.toolbarVisible)
        {
            toolbarFrame.origin.y = self.toolbarOriginY;
        }
        else
        {
            toolbarFrame.origin.y = self.toolbarOriginY - self.toolbar.frame.size.height;
        }
        self.toolbar.frame = toolbarFrame;
    }];
}

@end

The fade-out works just fine, except when I do anything else on the view. For example when I update a label, the toolbar snaps back to its initial state. I suppose some sort of internal refresh is triggered and the toolbar has an outdated model somewhere.

在此处输入图片说明

Solution: I still had Use Autolayout enabled despite the fact that I thought I had disabled it and removed all constraints before...

Thanks for this: Frame property of UIView is not getting assigned inside animation on iOS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM