iOS自定义形状导航栏 [英] iOS custom shape navigation bar
问题描述
我想开发带有自定义导航栏的应用,如下图所示:
I want to developer app with a custom navigation bar like in the following images:
我认为我需要子类UINavigationBar并添加按钮到导航栏的中心,但我真的不知道如何使导航栏看起来像图像。你能给我建议我该怎么办,任何类型文件的链接都很棒!
I think that i need to subclass UINavigationBar and add button to centre of nav bar, but i don't really know how to make navigation bar look like on image. Can you please give me advice what should i do, links to any kind of documentation would be awesome!
关于navBar的类似问题对我没有帮助:
Similar questions about navBar that doesn't helped me:
- ios back button in the bar
- Use custom Navigation Bar in iOS
- Custom Navigation Bar in iOS 5
- rogcar
编辑:
我的想法是下一步:使自定义导航栏高度略大于默认大小,并添加带箭头的背景图像,边缘有一些透明度。
My idea is next: make custom navigation bar height little bigger than default size, and add background image with arrow in it and with some transparency on the edges.
推荐答案
如果你想要一个按钮(你可能想要),你可以通过继承 UINavigationBar
来完成它。你应该记住高度
的 UINavigationBar
是只读属性。
If you want a button (you probably do want) you can achieve it completely by subclassing UINavigationBar
. You should remember that height
of UINavigationBar
is read-only property.
样式但不能点击:
所以我们假设我们将导航栏子类化并在那里添加按钮。你可以做到这一点,看起来会很棒。例如:
So let's assume we subclass the navigation bar and add button there. You could do this and it will be going look great. For example:
- (void)drawRect:(CGRect)rect
{
self.backgroundColor = [UIColor lightGrayColor];
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width/2-50, 0 , 100, 100)];
[myButton setBackgroundColor:[UIColor lightGrayColor]];
[myButton setTitle:@"Normal" forState:UIControlStateNormal];
[myButton setTitle:@"Highlighted" forState:UIControlStateHighlighted];
[self addSubview:myButton];
[self sendSubviewToBack:myButton];
}
但是你会遇到一个问题,你的按钮在<$ c下面是不可录制的$ C> UINvaigationBar 。 (我在答案的底部张贴了一张图片)
But you will facing a problem that your button is non tapeable below UINvaigationBar
. (I post an image on the bottom of the answer)
所以显然没有你想要遵循的路径。甚至不要尝试。
So there is clearly not a path you want to follow. Don't even try that.
风格但不可点击2:
您可以在导航栏子类中重写此方法
You may override this method in your navigation bar subclass
- (CGSize) sizeThatFits:(CGSize)size {
return CGSizeMake(custom_width, custom_height);
}
然后使用 UIBezierPath $ c对其进行掩码$ c>例如
正确(可点击)方式:
你必须创建一个视图棒到您的 UINavigationBar
。我将在这里做什么(如果你想要每个屏幕):
You have to create a view stick to your UINavigationBar
. What i will do here (if you want it to every screen) is:
- 制作一个
的类别UIViewController
可以绘制(例如 - 这是最简单的方法)UIButton
。 - 设置此'UIButton'无论如何你想要的(如果你想要的话)
- 将动作固定到'UIButton':
[btn addTarget:self action:@selector(menuShow :) forControlEvents:UIControlEventTouchUpInside];
-
menuShow:
方法应在类别中声明 - 每次想要重绘视图控制器时都可以调用绘图按钮。
- Make a Category of
UIViewController
which can draw (for example - this is easiest way)UIButton
. - Style this 'UIButton' whatever you want (if you want
- Pin action to 'UIButton':
[btn addTarget:self action:@selector(menuShow:) forControlEvents:UIControlEventTouchUpInside];
menuShow:
method should be declare in your category- You can call drawing button every time you want to redraw view controller.
正如你所看到的那样会有两个分隔视图: UINavigationBar
和 UIButton
。这允许您在此小按钮下设置内容并使其可以轻松。
As you can see there there will be two separates View: UINavigationBar
and UIButton
. This is allow you to set content under this little button and make it tapable.
那么为什么只是不隐藏导航栏,并使用不同的视图?因为iOS7;)当Apple在iOS7中更改它时,你必须重建ild你的伪NavigationBar,只有额外的视图,你不需要做任何事情。
So why just don't hide navigation bar, and use different view? Because iOS7 ;) When Apple change it in iOS7 for example then you have to rebuild your pseudo NavigationBar, with only additional view, you don't need to do anything.
这篇关于iOS自定义形状导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!