垂直对齐UINavigationItems [英] vertically aligning UINavigationItems

查看:102
本文介绍了垂直对齐UINavigationItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have customized the UINavigationBar's height to 100px and I'd like to add buttons onto this customized bar.

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

All is good except the button seems to want to sit on the bottom of the nav bar no matter what. I can't get it to align to the center or the top of the nav bar.

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

Here is the code; first I create a navigation controller in the app delegate and add one button on the right.

// 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];

然后我调整temp视图控制器中的导航栏大小如下:

then I resize the navigation bar in the "temp" view controller like so:

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

我还尝试向rightBarButtonItem添加自定义视图但我可以没有添加自定义视图来完全触摸顶部。

I've also tried to add a custom view to the rightBarButtonItem but I can't get the added custom view to touch the top completely.

此次不幸尝试的代码:

// 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];

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

Does anyone know how to vertically align UIBarButtonItems on a UINavigationBar?

推荐答案

选项1:


  • 创建UINavigationBar子类

  • 在此类中覆盖 - (void)layoutSubviews 您将在其中重新定位UIBarButtonItems

  • 在Interface Builder中将UINavigationBar类设置为UINavigationBar子类

  • create UINavigationBar subclass
  • inside this class override - (void)layoutSubviews where you will reposition UIBarButtonItems
  • in Interface Builder set UINavigationBar class to UINavigationBar subclass

示例:

在UINavigationBar的子类中:

inside subclass of 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;
    }
}

查看控制器:

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

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

Option 2 (inserting UIBarButtonItem doesn't work):

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

这篇关于垂直对齐UINavigationItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆