摆脱UINavigationBar右侧的空间 [英] Get rid of the space on the right side of a UINavigationBar

查看:91
本文介绍了摆脱UINavigationBar右侧的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这就是我想要完成的事情:

So, this is what I'm trying to accomplish:

这是 UINavigationBar ,带有 UIBarButtonItem 使用自定义 UIButton 初始化。

基本上是这样的:

It's a UINavigationBar with a UIBarButtonItem that gets initialized with a custom UIButton.
Basically like this:

UIButton *favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[favoriteButton addTarget:target
                   action:action
         forControlEvents:UIControlEventTouchUpInside];
favoriteButton.frame = CGRectMake(0.0f, 0.0f, 44.0f, 44.0f);

UIImage *backgroundImage = [[UIImage imageNamed:@"favorite-button-background-orange"]
                    resizableImageWithCapInsets:UIEdgeInsetsMake(0, 2, 0, 0)];
[favoriteButton setBackgroundImage:backgroundImage
                          forState:UIControlStateNormal];

UIBarButtonItem *favoriteButtonItem = [[UIBarButtonItem alloc] initWithCustomView:favoriteButton];

问题是我似乎无法摆脱右侧那个令人讨厌的小边框 UINavigationBar

The problem is I can't seem to get rid of that nasty little border on the right hand side of the UINavigationBar.

我尝试设置自定义 TitleView ,如如何删除UIBarButtonItem旁边的填充但这并没有改变一件事。

I tried setting a custom TitleView as described in "How to remove padding next to a UIBarButtonItem" but this didn't change a thing.

我还尝试添加另一个负宽度的UIBarButtonItem(正如我在阅读的一些博文中所建议的那样),但这也没有帮助。

I also tried adding another UIBarButtonItem with a negative width (as suggested in some blogposts I read) but that didn't help either.

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                 target:nil
                 action:nil];
negativeSpacer.width = -5.0f;
self.navigationItem.rightBarButtonItems = %[favoriteButtonItem, negativeSpacer];

我认为iOS不会让我改变它,因为圆形边界保留一些(不可避免的?空间/填充?
所以我的问题是。我该如何解决这个限制?我是否需要创建自定义 UINavigationBar 类?我可以通过修改 UIBarButtonItem 以某种方式伪造它吗?我只是使用了错误的组件吗?

I assume iOS won't let me change it because the "rounded border" reserves some (unavoidable?) space / padding? So my question is. How should I work around this limitation? Do I need to create a custom UINavigationBar class? Can I fake it somehow by modifying the UIBarButtonItem? Am I just using the wrong component?

推荐答案

有两种解决方案。 (可能不是确切的解决方案)

1)您应该继承 UINavigationBar 并且玩弄它。例如,您可能需要在右上角中自己绘制 UIBarButtonItem

1) You should subclass UINavigationBar and play around with it. For example you may have to draw your UIBarButtonItem yourself in the right corner.

2)而不是使用 UINavigationBar ,使用 UIView 并添加子视图,使其看起来像 UINavigationBar 并使用它。

2) Instead of using UINavigationBar, Use UIView and add subviews to make it exactly look like the UINavigationBar and use it.

我希望第二次解决方案很容易在您的要求中实现。我认为没有任何其他方法可以达到您的要求。

I hope second solution would be easy to achieve in your requirement. I don't think there would be any other way to achieve your requirement.

这篇关于摆脱UINavigationBar右侧的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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