简体   繁体   English

iOS自定义形状导航栏

[英]iOS custom shape navigation bar

I want to developer app with a custom navigation bar like in the following images: 我想用自定义导航栏开发应用程序,如下图所示:

在此输入图像描述

在此输入图像描述

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. 我认为我需要子类UINavigationBar并添加按钮到导航栏的中心,但我真的不知道如何使导航栏看起来像在图像上。 Can you please give me advice what should i do, links to any kind of documentation would be awesome! 能否请你给我建议我应该做什么,任何类型的文件链接都很棒!

Similar questions about navBar that doesn't helped me: 关于navBar的类似问题对我没有帮助:

EDIT: 编辑:

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. 我的想法是下一步:使自定义导航栏高度略大于默认大小,并添加带箭头的背景图像,边缘有一些透明度。

If you want a button (you probably do want) you can achieve it completely by subclassing UINavigationBar . 如果你想要一个按钮(你可能想要),你可以通过UINavigationBar来完成它。 You should remember that height of UINavigationBar is read-only property. 您应该记住UINavigationBar height是只读属性。

Style but not tappable: 风格但不可点击:

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

But you will facing a problem that your button is non tapeable below UINvaigationBar . 但是你会遇到一个问题,你的按钮在UINvaigationBar下面是不可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. 甚至不尝试。

Style but not tappable 2: 风格但不适合2:

You may override this method in your navigation bar subclass 您可以在导航栏子类中覆盖此方法

- (CGSize) sizeThatFits:(CGSize)size  {
  return CGSizeMake(custom_width, custom_height);
}

And then mask it using UIBezierPath for example 然后使用UIBezierPath进行掩码

The right (tappable) way: 正确(可点击)方式:

You have to create a view stick to your UINavigationBar . 您必须为UINavigationBar创建一个视图棒。 What i will do here (if you want it to every screen) is: 我将在这里做什么(如果你想在每个屏幕上)是:

  1. Make a Category of UIViewController which can draw (for example - this is easiest way) UIButton . 创建一个可以绘制的UIViewController类别(例如 - 这是最简单的方法) UIButton
  2. Style this 'UIButton' whatever you want (if you want 无论你想要什么样的风格'UIButton'(如果你想要的话)
  3. Pin action to 'UIButton': [btn addTarget:self action:@selector(menuShow:) forControlEvents:UIControlEventTouchUpInside]; 将动作固定为'UIButton': [btn addTarget:self action:@selector(menuShow:) forControlEvents:UIControlEventTouchUpInside];
  4. menuShow: method should be declare in your category menuShow:方法应该在您的类别中声明
  5. You can call drawing button every time you want to redraw view controller. 每次要重绘视图控制器时,都可以调用绘图按钮。

As you can see there there will be two separates View: UINavigationBar and UIButton . 正如你所看到的,会有两个独立的视图: UINavigationBarUIButton This is allow you to set content under this little button and make it tapable. 这允许您在此小按钮下设置内容并使其可点击。

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. 因为iOS7;)当Apple在iOS7中更改它时,你必须重建你的伪NavigationBar,只有额外的视图,你不需要做任何事情。

在此输入图像描述

You do not need to subclass UINavigationBar. 您不需要子类UINavigationBar。 Create UIView add to it UIImageView as background with image in the shape you need, add button. 创建UIView添加到它UIImageView作为背景与您需要的形状的图像,添加按钮。

Subclass UINavigationController hide UINavigationBar, add custom navigation bar. 子类UINavigationController隐藏UINavigationBar,添加自定义导航栏。

First Hide navigation bar using - 首先隐藏导航栏使用 -

self.navigationController.navigationBarHidden = YES;

Then create UIView with required height,height of navigationBar is 44px.Then create background image view, object of required UIButton and add all objects on created UIView as a subview.It will look like navigationBar.Thank you. 然后创建具有所需高度的UIView,navigationBar的高度为44px。然后创建背景图像视图,所需UIButton的对象,并将创建的UIView上的所有对象添加为子视图。它看起来像navigationBar.Thank you。

You can add your custom shaped view as titleView on the navigation bar. 您可以在导航栏上将自定义形状视图添加为titleView。 Just make sure that clipsToBounds is set to NO, so it doesn't get clipped. 只需确保clipsToBounds设置为NO,因此不会被剪裁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM