UINavigationBar自定义导航栏


 

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


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

 

 

Objective-c代码 复制代码  收藏代码
  1. //CustomNavigationBar.h   
  2. @interface UINavigationBar (UINavigationBarCategory)   
  3. UIImageView *backgroundView;   
  4. - (void)setBackgroundImage:(UIImage*)image;   
  5. - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;   
  6. @end   
  7.   
  8. //CustomNavigationBar.m   
  9. @implementation UINavigationBar (UINavigationBarCategory)   
  10. -(void)setBackgroundImage:(UIImage*)image   
  11. {   
  12.     if(image == nil)   
  13.     {   
  14.         [backgroundView removeFromSuperview];   
  15.     }   
  16.     else   
  17.     {   
  18.         backgroundView = [[UIImageView alloc] initWithImage:image];   
  19.         backgroundView.tag = 1;   
  20.         backgroundView.frame = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);   
  21.         backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;   
  22.         [self addSubview:backgroundView];   
  23.         [self sendSubviewToBack:backgroundView];   
  24.         [backgroundView release];   
  25.     }   
  26. }   
  27.   
  28. //for other views   
  29. - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index   
  30. {   
  31.     [super insertSubview:view atIndex:index];   
  32.     [self sendSubviewToBack:backgroundView];   
  33. }   
  34. @end   
  35.   
  36. //YourViewController.m   
  37. - (void)viewWillAppear:(BOOL)animated   
  38. {   
  39.     [super viewWillAppear:animated];   
  40.     [self.navigationController.navigationBar   
  41.         setBackgroundImage:[UIImage imageNamed:@"navigation_bar_bg.png"]];   
  42. }  
  • CustomNavigationBar.zip (54.1 KB) 

你可能感兴趣的:(apple,image,UIView,interface)