垂直对齐 UINavigationItems

发布于 2024-11-18 07:46:31 字数 1978 浏览 3 评论 0原文

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

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

在此处输入图像描述

这是代码;首先,我在应用程序委托中创建一个导航控制器,并在右侧添加一个按钮。

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

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

- (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 添加自定义视图,但无法让添加的自定义视图完全触摸顶部。

在此处输入图像描述

以及此不幸尝试的代码:

// 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 上垂直对齐 UIBarButtonItems 吗?

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.

enter image description here

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

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

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.

enter image description here

And the code for this unfortunate attempt:

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

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

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

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

发布评论

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

评论(4

因为看清所以看轻 2024-11-25 07:46:31

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

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];

This will move page title and all buttons up 20 px:

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];
与之呼应 2024-11-25 07:46:31

如果您只有 rightBarButtonItem 而没有 left,则当前接受的 alhcr 答案(选项 1)不起作用。它还对 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];
        }
    }
}

The current accepted answer from alhcr (Option 1) does not work if you only have a rightBarButtonItem and no left. It is also making an assumption about the subview ordering of the UINavigationBar which is not guaranteed and could very well change in a future version of iOS. Here is a better way of customizing navigation button frames:

// 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];
        }
    }
}
聚集的泪 2024-11-25 07:46:31

选项 1:

  • 创建 UINavigationBar 子类
  • 在此类覆盖 - (void)layoutSubviews
  • ,您将在 Interface Builder 中重新定位 UIBarButtonItems 将 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;
    }
}

视图控制器:

- (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 不起作用):

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

Option 1:

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

Example:

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

view controller:

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

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];
只是在用心讲痛 2024-11-25 07:46:31

我通过调整自定义按钮的图像边缘插入找到了这个问题的解决方案。我在应用程序中要求增加导航栏的高度,增加高度后会导致 rightBarButtonItem 和 leftBarButtonItem 图像未定位问题。

找到下面的代码:-

UIImage *image = [[UIImage imageNamed:@"searchbar.png"];
UIButton* searchbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[searchbutton addTarget:self action:@selector(searchBar:) forControlEvents:UIControlEventTouchUpInside]; 
searchbutton.frame = CGRectMake(0,0,22, 22);
[searchbutton setImage:image forState:UIControlStateNormal];
[searchbutton setImageEdgeInsets:UIEdgeInsetsMake(-50, 0,50, 0)];
// Make BarButton Item
 UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithCustomView:searchbutton];
self.navigationItem.rightBarButtonItem = navItem;

I found the solution of this problem by making adjustment in the Image Edge Insets of the custom button. I had the requirement in the app to increase the height of the navigation bar and after increasing the height makes the rightBarButtonItem and leftBarButtonItem images unpositioned problem.

Find the code below:-

UIImage *image = [[UIImage imageNamed:@"searchbar.png"];
UIButton* searchbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[searchbutton addTarget:self action:@selector(searchBar:) forControlEvents:UIControlEventTouchUpInside]; 
searchbutton.frame = CGRectMake(0,0,22, 22);
[searchbutton setImage:image forState:UIControlStateNormal];
[searchbutton setImageEdgeInsets:UIEdgeInsetsMake(-50, 0,50, 0)];
// Make BarButton Item
 UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithCustomView:searchbutton];
self.navigationItem.rightBarButtonItem = navItem;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文