垂直对齐UINavigationItems

Jih*_*ang 7 objective-c uinavigationbar uinavigationcontroller uinavigationitem ios

我已经将UINavigationBar的高度定制为100px,我想在这个自定义栏上添加按钮.

一切都很好,除了按钮似乎想要坐在导航栏的底部,无论如何.我不能让它与导航栏的中心或顶部对齐.

在此输入图像描述

这是代码; 首先,我在app delegate中创建一个导航控制器,并在右侧添加一个按钮.

// set main navigation controller
temp *tempc = [[temp alloc] initWithNibName:@"temp" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tempc];

UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];

[tempc.navigationItem setRightBarButtonItem:navItem];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)

然后我在"临时"视图控制器中调整导航栏的大小,如下所示:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, 100.0f);
    [self.navigationController.navigationBar setFrame:frame];
    [self.navigationController.navigationBar setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
}
Run Code Online (Sandbox Code Playgroud)

我还尝试向rightBarButtonItem添加自定义视图,但我无法将添加的自定义视图完全触及顶部.

在此输入图像描述

这个不幸的尝试的代码:

// set main navigation controller
temp *tempc = [[temp alloc] initWithNibName:@"temp" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tempc];

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36.0f, 80.0f)];
[customView setBackgroundColor:[UIColor blueColor]];

UIButton *customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setFrame:CGRectMake(0, 22.0f, 36.0f, 36.0f)];
[customButton setTitle:@"+" forState:UIControlStateNormal];

[customView addSubview:customButton];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
Run Code Online (Sandbox Code Playgroud)

有谁知道如何在UINavigationBar上垂直对齐UIBarButtonItems?

kra*_*g22 13

这会将页面标题和所有按钮向上移动20像素:

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)


nrj*_*nrj 12

alhcr(选项1)当前接受的答案如果你只剩下一个rightBarButtonItem而没有剩下,则不起作用.它还对UINavigationBar的子视图排序做出了一个假设,这个假设并不能得到保证,并且在未来的iOS版本中可以很好地改变.这是一种自定义导航按钮框架的更好方法:

// UINavigationBar subclass

- (void)layoutSubviews {

    [super layoutSubviews];

    UINavigationItem *navigationItem = [self topItem];

    for (UIView *subview in [self subviews]) {

        if (subview == [[navigationItem rightBarButtonItem] customView]) {

            CGRect newRightButtonRect = CGRectMake(...new rect...);
            [subview setFrame:newRightButtonRect];
        }
        else if (subview == [[navigationItem backBarButtonItem] customView]) {

            CGRect newBackButtonRect = CGRectMake(...new rect...);
            [subview setFrame:newBackButtonRect];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


alh*_*hcr 7

选项1:

  • 创建UINavigationBar子类
  • 在此类中覆盖- (void)layoutSubviews您将重新定位UIBarButtonItems的位置
  • 在Interface Builder中将UINavigationBar类设置为UINavigationBar子类

例:

UINavigationBar的子类内部:

- (void)layoutSubviews {
    int index = 0;
    for ( UIButton *button in [self subviews] ) {
        if(index == 1) {
            [button setFrame:CGRectMake(5,40,60,40)]; //leftBarButtonItem
        } else if(index == 2) {
            [button setFrame:CGRectMake(275,40,40,40)]; //rightBarButtonItem
        }
        ++index;
    }
}
Run Code Online (Sandbox Code Playgroud)

视图控制器:

- (void)viewDidLoad {
...
    UIBarButtonItem *navItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
    UIBarButtonItem *navItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil];
    [self.navigationItem setRightBarButtonItem:navItem1];   
    [self.navigationItem setLeftBarButtonItem:navItem2];

    CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, 100.0f);
    [self.navigationController.navigationBar setFrame:frame];
...
}
Run Code Online (Sandbox Code Playgroud)

选项2(插入UIBarButtonItem不起作用):

UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,320,100)];
[self.view addSubview:navigationBar];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0,25,50,50)];
[navigationBar addSubview:button];
Run Code Online (Sandbox Code Playgroud)