【问题标题】:Custom Navigation Bar in ios6 and ios7ios6 和 ios7 中的自定义导航栏
【发布时间】:2014-05-11 18:24:06
【问题描述】:

我有一个问题,我现在找不到解决方案。在我的应用程序中,我的 NavigationBar 中有多个按钮,这些按钮在整个应用程序中都是必需的,而不是在每个视图控制器中创建按钮,我想创建 UINavigationBar 或 UINavigationController 的子类(我不知道是哪一个)。因此,每当用户在视图之间移动时,导航栏总是包含这些按钮。到目前为止,我已经对此进行了很多搜索,但找不到任何有价值的东西。 请建议我这样做,提前谢谢。

【问题讨论】:

    标签: ios iphone ipad uinavigationcontroller uinavigationbar


    【解决方案1】:

    你可以继承标准UINavigationBar来实现这个

    @interface CustomNavigationBar : UINavigationBar
    
    - (id)initWithFrame:(CGRect)frame;
    
    @end
    
    @implementation CustomNavigationBar
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
    
            UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
            [btn addTarget:self action:@selector(<#selector#>) forControlEvents:<#(UIControlEvents)#>]
            [self addSubview:btn];
    
            ...
            ...
    
    
        }
        return self;
    }
    - (void)drawRect:(CGRect)rect {
    
        [[UIColor redColor] setFill];
        UIRectFill(rect);
    }
    
    @end
    

    要使用 StoryBoard 或 xib,只需将标准类名称更改为 CustomNavigationBar。

    或

    如果您希望以编程方式进行

    在AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    
        self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    
        UINavigationController *navVC = [[UINavigationController alloc] initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil];
    
        UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
        ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:@"VC"];
    
    
        [navVC setViewControllers:[NSArray arrayWithObject:VC1] animated:NO];
        [self. window setRootViewController:navVC];
    
        [self. window makeKeyAndVisible];
    
        return YES;
    }
    

    【讨论】:

      【解决方案2】:
      #import "CustomNavBar.h"
      
      @implementation CustomNavBar
      
      - (id)initWithFrame:(CGRect)frame
      {
          self = [super initWithFrame:frame];
          if (self) {
              // Initialization code
              self.tintColor=[UIColor greenColor];
          }
          return self;
      }
      - (void)drawRect:(CGRect)rect {
          UIImage *image = [UIImage imageNamed:@"Custom-Nav-Bar-BG.png"];
          [image drawInRect:CGRectMake(0, 0, 40, self.frame.size.height)];
      
          UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
          [btn drawRect:CGRectMake(42, 0, 40, self.frame.size.height )];
      
          UIButton *btn2=[UIButton buttonWithType:UIButtonTypeContactAdd];
          [btn2 drawRect:CGRectMake(82, 0, 40, self.frame.size.height )];
      }
      
      
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-24
        • 2014-01-31
        • 2013-09-29
        • 2012-03-12
        相关资源
        最近更新 更多