`
逍遥一狂
  • 浏览: 188533 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

UINavigationBar自定义背景以及按钮

阅读更多

iOS技术群:176078249

 

UINavigationBar自定义导航栏背景和按钮,完美支持横屏竖屏旋转,视图控制器可以分别使用自己的导航栏


此方法可以通过Apple审核,导航上的按钮背景需要做,否则看起来不那么和之又谐

 

 

//CustomNavigationBar.h
@interface UINavigationBar (UINavigationBarCategory)
UIImageView *backgroundView;
- (void)setBackgroundImage:(UIImage*)image;
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
@end

//CustomNavigationBar.m
@implementation UINavigationBar (UINavigationBarCategory)
-(void)setBackgroundImage:(UIImage*)image
{
	if(image == nil)
	{
		[backgroundView removeFromSuperview];
	}
	else
	{
		backgroundView = [[UIImageView alloc] initWithImage:image];
		backgroundView.tag = 1;
		backgroundView.frame = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);
		backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
		[self addSubview:backgroundView];
		[self sendSubviewToBack:backgroundView];
		[backgroundView release];
	}
}

//for other views
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
	[super insertSubview:view atIndex:index];
	[self sendSubviewToBack:backgroundView];
}
@end

//YourViewController.m
- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self.navigationController.navigationBar
		setBackgroundImage:[UIImage imageNamed:@"navigation_bar_bg.png"]];
}
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics