iPhone: UINavigationBar with buttons - adjust the

2019-02-03 16:44发布

I am working on an iPhone application which works in both orientations: portrait and landscape.

I am using for one view and tableview embedded in an UINavigationController. The height of this navigationbar with its buttons is either: 44px portrait or 34px landscape.

Within a different view I created the UINavigationBar by myself and I am able to set the frame for the correct size, but the embedded UINavigationItem with UIBarButtonItem doesn't shrink. So for the 34 px in the landscape mode this button is to big and overlaps the navbar in the height.

The funny thing is though, that this worked with identical code in other apps... have no idea which it isn't here.

Is there anyway to adjust the height / position of an UIBarButtonItem?

Here is the code snippet:

    navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 34.0f)];
[navBar setBarStyle: UIBarStyleBlackOpaque];

[self addSubview: navBar];

barButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"flip", @"flip") style:UIBarButtonItemStylePlain target:self action:@selector(flip)];

item = [[UINavigationItem alloc] initWithTitle: NSLocalizedString(@"Translation", @"Translation Tab Bar Title")];
[item setRightBarButtonItem: barButton];
[navBar pushNavigationItem:item animated:NO];   

alt text http://labs.kiesl.eu/images/navbar.png

Thanks

Tom

3条回答
不美不萌又怎样
2楼-- · 2019-02-03 17:42

I figured it out: The height of the navigation bar must be 32 px! With 33 or 34 px the alignment screws up.

查看更多
对你真心纯属浪费
3楼-- · 2019-02-03 17:45

Here's the code I wrote based on crashtesttommy's answer:

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

- (void) correctNavBarHeightForOrientation:(UIInterfaceOrientation)orientation {
    // This is only needed in on the iPhone, since this is a universal app, check that first.
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone){
        if (UIInterfaceOrientationIsLandscape(orientation)) {
            self.navBar.frame = CGRectMake(self.navBar.frame.origin.x, self.navBar.frame.origin.y, self.navBar.frame.size.width, 32.0f);
        } else {
            self.navBar.frame = CGRectMake(self.navBar.frame.origin.x, self.navBar.frame.origin.y, self.navBar.frame.size.width, 44.0f);
        }
    }  
}

- (void) viewWillAppear:(BOOL)animated {
    [self correctNavBarHeightForOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self correctNavBarHeightForOrientation:toInterfaceOrientation];
}
查看更多
干净又极端
4楼-- · 2019-02-03 17:50

Create alternate images, and in the view controller methods that handle animating the rotation, change the images in the bar button items. Or you could probably use a UIImageView as a custom view in your item, and set its content mode to scale the image down.

查看更多
登录 后发表回答