iPhone:隐藏/显示工具栏

发布于 2024-12-18 14:23:54 字数 86 浏览 2 评论 0原文

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

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

I have a toolbar on top of my view with a back button. I would like when the view loads to appear with the toolbar hidden and then, with a touch of the button to appear animated.

--Edit--
I am not using a Navigation Controller.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一张白纸 2024-12-25 14:23:54

在显示视图之前:

[self.navigationController setToolbarHidden:YES];

当您按下按钮时:

[self.navigationController setToolbarHidden:NO];

Before the view is shown:

[self.navigationController setToolbarHidden:YES];

When you press the button:

[self.navigationController setToolbarHidden:NO];
饮湿 2024-12-25 14:23:54

给出新信息 - 没有 UINavigationController - 情况有所不同。以下是我的代码中的相关部分...

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

// Create the navigation bar

self.navBar = [[UINavigationBar alloc] init];
[self.view addSubview:self.navBar];

布局...

- (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;
}

创建一个函数来控制/隐藏它:

- (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;
                         }
         ];
    }
}

在哪里

#define NAVBARHEIGHT 44

Given new information - that there's no UINavigationController - things are different. Here are the relevant bits from my code...

Create the nav bar and add it to your view:

// Create the navigation bar

self.navBar = [[UINavigationBar alloc] init];
[self.view addSubview:self.navBar];

Lay it out..

- (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;
}

Create a function to who it/hide it:

- (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;
                         }
         ];
    }
}

where

#define NAVBARHEIGHT 44
败给现实 2024-12-25 14:23:54

以下是 tarmes 的一些移植到 MonoTouch/c# 的代码。

    public static RectangleF FrameForOrientation(UIInterfaceOrientation orientation) {
        var screen = UIScreen.MainScreen;
        var fullScreenRect = screen.Bounds;      // always implicitly in Portrait orientation.
        var 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.
        var statusBarHeight = Math.Max((fullScreenRect.Width - appFrame.Width), (fullScreenRect.Height- appFrame.Height));

        // Initially assume portrait orientation.
        var width = fullScreenRect.Width;
        var height = fullScreenRect.Height;

        // Correct for orientation.
        if (IsLandscapeOrientation(orientation)) {
            width = fullScreenRect.Height;
            height = fullScreenRect.Width;
        }

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

        return new RectangleF(0, statusBarHeight, width, height);
    }

    public static SizeF SizeForOrientation(UIInterfaceOrientation orientation) {
        var frame = FrameForOrientation(orientation);
        return new SizeF(frame.Width, frame.Height);
    }


    public static bool IsLandscapeOrientation(UIInterfaceOrientation orientation) {
        return 
            orientation == UIInterfaceOrientation.LandscapeLeft ||
            orientation == UIInterfaceOrientation.LandscapeRight;
    }

Here's some of tarmes's code ported to MonoTouch/c#.

    public static RectangleF FrameForOrientation(UIInterfaceOrientation orientation) {
        var screen = UIScreen.MainScreen;
        var fullScreenRect = screen.Bounds;      // always implicitly in Portrait orientation.
        var 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.
        var statusBarHeight = Math.Max((fullScreenRect.Width - appFrame.Width), (fullScreenRect.Height- appFrame.Height));

        // Initially assume portrait orientation.
        var width = fullScreenRect.Width;
        var height = fullScreenRect.Height;

        // Correct for orientation.
        if (IsLandscapeOrientation(orientation)) {
            width = fullScreenRect.Height;
            height = fullScreenRect.Width;
        }

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

        return new RectangleF(0, statusBarHeight, width, height);
    }

    public static SizeF SizeForOrientation(UIInterfaceOrientation orientation) {
        var frame = FrameForOrientation(orientation);
        return new SizeF(frame.Width, frame.Height);
    }


    public static bool IsLandscapeOrientation(UIInterfaceOrientation orientation) {
        return 
            orientation == UIInterfaceOrientation.LandscapeLeft ||
            orientation == UIInterfaceOrientation.LandscapeRight;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文