iPhone:隐藏/显示工具栏

Pan*_*ios 9 iphone xcode toolbar show-hide ios

我的视图顶部有一个带有后退按钮的工具栏.我希望当视图加载时隐藏工具栏然后,只需按一下按钮即可显示动画.

- 编辑 - 我没有使用导航控制器.

tar*_*mes 11

在显示视图之前:

[self.navigationController setToolbarHidden:YES];
Run Code Online (Sandbox Code Playgroud)

按下按钮时:

[self.navigationController setToolbarHidden:NO];
Run Code Online (Sandbox Code Playgroud)


tar*_*mes 7

鉴于新信息 - 没有UINavigationController - 事情是不同的.以下是我的代码中的相关位...

创建导航栏并将其添加到视图中:

// Create the navigation bar

self.navBar = [[UINavigationBar alloc] init];
[self.view addSubview:self.navBar];
Run Code Online (Sandbox Code Playgroud)

把它放出来......

- (CGRect)frameForOrientation:(UIInterfaceOrientation)theOrientation
{
    UIScreen *screen = [UIScreen mainScreen];
    CGRect fullScreenRect = screen.bounds;      // always implicitly in Portrait orientation.
    CGRect appFrame = screen.applicationFrame;

    // Find status bar height by checking which dimension of the applicationFrame is narrower than screen bounds.
    // Little bit ugly looking, but it'll still work even if they change the status bar height in future.
    float statusBarHeight = MAX((fullScreenRect.size.width - appFrame.size.width), (fullScreenRect.size.height - appFrame.size.height));

    // Initially assume portrait orientation.
    float width = fullScreenRect.size.width;
    float height = fullScreenRect.size.height;

    // Correct for orientation.
    if (UIInterfaceOrientationIsLandscape(theOrientation)) {
        width = fullScreenRect.size.height;
        height = fullScreenRect.size.width;
    }

    // Account for status bar, which always subtracts from the height (since it's always at the top of the screen).
    height -= statusBarHeight;

    return CGRectMake(0, statusBarHeight, width, height);
}

- (CGSize)viewSizeForOrientation:(UIInterfaceOrientation)theOrientation
{
    CGRect frame = [self frameForOrientation:theOrientation];
    return CGSizeMake(frame.size.width, frame.size.height);
}

- (void)layoutSubviewsForInterfaceOrientation:(UIInterfaceOrientation)theOrientation withAnimation:(BOOL)animate
{
    CGSize fullSize = [self viewSizeForOrientation:theOrientation];
    float width = fullSize.width;
    float height = fullSize.height;

    CGRect newFrame = CGRectMake(0, 0, width, height);
    SubViewController *controller;
    UIView *theView;

    // Place the navigation bar

    CGRect navBarFrame = newFrame;
    navBarFrame.size.height = NAVBARHEIGHT;
    self.navBar.frame = navBarFrame;
}
Run Code Online (Sandbox Code Playgroud)

创建一个函数给它/隐藏它:

- (void)showNavigationBar:(BOOL)show
{
    if (show == YES && self.navBar.hidden == YES) {

        // Move the frame out of sight
        CGRect frame = self.navBar.frame;
        frame.origin.y = -frame.size.height;
        self.navBar.frame = frame;

        // Display it nicely
        self.navBar.hidden = NO;
        frame.origin.y = 0.0;
        [self.view bringSubviewToFront:self.navBar];

        [UIView animateWithDuration:0.3
                         animations:^(void) {
                             self.navBar.frame = frame;
                         }
         ];
    }
    else if (show == NO && self.navBar.hidden == NO) {

        CGRect frame = self.navBar.frame;

        // Display it nicely
        frame.origin.y = -frame.size.height;
        [self.view bringSubviewToFront:self.navBar];

        [UIView animateWithDuration:0.3
                         animations:^(void) {
                             self.navBar.frame = frame;
                         }
                         completion:^(BOOL finished) {
                             self.navBar.hidden = YES;
                         }
         ];
    }
}
Run Code Online (Sandbox Code Playgroud)

哪里

#define NAVBARHEIGHT 44
Run Code Online (Sandbox Code Playgroud)