【问题标题】:I need a few information about UINavigationControllers/UIBarButtonItems我需要一些关于 UINavigationControllers/UIBarButtonItems 的信息
【发布时间】:2012-05-23 17:19:36
【问题描述】:

我想这个问题对于这里的很多人来说可能听起来很无聊。但我仍然是 iOS 开发的初学者,即使我正在改进,有些事情让我感到困惑。 我的应用程序的菜单具有以下配置:包含导航栏的导航控制器,并嵌入包含导航项的视图控制器。 我的导航栏子类 UINavigationBar 使其看起来像我想要的那样。

现在,我只想添加一个带有 i(信息)的自定义 BarButtonItem,我已经绘制了它。所以这是我的问题:如何添加此按钮?

非常感谢您的建议。

【问题讨论】:

    标签: objective-c ios uinavigationcontroller uibarbuttonitem


    【解决方案1】:
    // Create the Info button
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    
    // Give it an action handler
    [infoButton addTarget:self 
                   action:@selector(infoButtonAction:)
         forControlEvents:UIControlEventTouchUpInside];
    
    // Create a UIBarButtonItem to "contain" the Info button
    UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
    
    // Add the UIBarButtonItem to the navigation bar (on the left side)
    [self.navigationItem setLeftBarButtonItem:infoItem animated:NO];
    

    动作处理程序:

    - (void)infoButtonAction:(id)sender {
        NSLog(@"Tapped on info button!");
    }
    

    【讨论】:

    • 谢谢...我想我必须在我的 ViewController 的 viewDidLoad 方法中写这个?
    【解决方案2】:

    这可能不能直接回答您的问题,但如果您使用 UINavigationController 可能会避免出现问题,因为它本来就是要使用的。与其继承 UINavigationBar 来自定义它的界面,不如使用它的 UIAppearance 方法,然后像往常一样创建一个 UINavigationController。

    // Create navigation controller.
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
    
    // Customize navigation bar appearance.
    [[UINavigationBar appearance] setTintColor:[UIColor blueColor]];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation-bar-background"] forBarMetrics:UIBarMetricsDefault];
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多