Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

iphone - iOS custom shape navigation bar

I want to developer app with a custom navigation bar like in the following images:

enter image description here

enter image description here

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!

Similar questions about navBar that doesn't helped me:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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. (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:

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

The right (tappable) way:

You have to create a view stick to your 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.
  2. Style this 'UIButton' whatever you want (if you want
  3. Pin action to 'UIButton': [btn addTarget:self action:@selector(menuShow:) forControlEvents:UIControlEventTouchUpInside];
  4. menuShow: method should be declare in your category
  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. 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.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...