【问题标题】:reusable UIBarButtonItem in all View Controllers in Objective CObjective C 中所有视图控制器中的可重用 UIBarButtonItem
【发布时间】:2016-04-01 14:24:30
【问题描述】:

我使用背景颜色和字体大小制作自定义导航栏。此外,我在右侧添加了菜单按钮。为此,我将类别命名为

UINavigationController+Transparent.h

 @interface UINavigationController (Transparent)
   - (void)presentTransparentNavigationBar;
   - (void)hideTransparentNavigationBar;
 @end

UINavigationController+Transparent.m

#import "UINavigationController+Transparent.h"

@implementation UINavigationController (Transparent)

UIBarButtonItem *menuButton;

- (void)presentTransparentNavigationBar;
{
menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(showMenu:)];

    [menuButton setTintColor:[UIColor whiteColor]];

    self.navigationItem.rightBarButtonItem = menuButton;


UIImage *backButtonImage = [UIImage imageNamed:@"back"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

[backButton setImage:backButtonImage
            forState:UIControlStateNormal];

backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);

[backButton addTarget:self
               action:@selector(popViewController)
     forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;


    [self.navigationBar setTranslucent:NO];
    [self.navigationBar setShadowImage:[UIImage new]];
    [self.navigationBar setBarTintColor:[UIColor wolfRed]];
    [self.navigationBar setTitleTextAttributes:
        @{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:19.0] }];
    [self setNavigationBarHidden:NO animated:NO];
}


- (void)hideTransparentNavigationBar
{
  [self setNavigationBarHidden:YES animated:NO];
  [self.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
  [self.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]];
  [self.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]];
}
@end

在我调用的 ViewControllers 中

[self.navigationController presentTransparentNavigationBar];

或

[self.navigationController hideTransparentNavigationBar];

问题是菜单按钮不可见,后退按钮看起来像标准的 iOS 蓝色后退按钮。 感谢您的帮助!

【问题讨论】:

    标签: ios objective-c uinavigationcontroller uinavigationbar


    【解决方案1】:

    请参阅我曾经以以下方式自定义导航栏 目标c

    Step.1 确保您的视图控制器嵌入到导航控制器中

    Step.2 在您的资产中添加所需的图标,例如。 “menu_icon”和“返回”

    Step.3 使用 Objective-C,您可以根据需要自定义导航栏。

    我总是这样,

    #import "ViewController.h"
    @interface ViewController (){UIBarButtonItem *menuButton;}
    @end
    
    
    @implementation ViewController
    
    #pragma mark - life cycle methods
    - (void)viewDidLoad {[super viewDidLoad];}
    - (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}
    
    #pragma mark - IBAction methods
    - (IBAction)showClicked:(id)sender {[self presentTransparentNavigationBar];}
    - (IBAction)hideClicked:(id)sender {[self hideTransparentNavigationBar];}
    
    #pragma mark - Navigation Bar methods
    
    //Customize Naviagtion Bar
    - (void)presentTransparentNavigationBar;
    {
        //Add Menu Button to Navigaiton Bar
        menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(showMenu:)];
        [menuButton setTintColor:[UIColor whiteColor]];
        self.navigationItem.rightBarButtonItem = menuButton;
        
        
        //Add Back button to Navigation Bar
        UIImage *backButtonImage = [UIImage imageNamed:@"back"];
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setImage:backButtonImage
                    forState:UIControlStateNormal];
        backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
        [backButton addTarget:self
                       action:@selector(popViewController)
             forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.navigationItem.leftBarButtonItem = backBarButtonItem;
        
        //Config Navigaton bar settings
        [self.navigationController.navigationBar setTranslucent:NO];
        [self.navigationController.navigationBar setShadowImage:[UIImage new]];
        [self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];//I used light gray color
        [self.navigationController.navigationBar setTitleTextAttributes:
         @{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:19.0] }];
        [self.navigationController.navigationBar setHidden:NO];
    }
    
    
    //Hide Navigation Bar
    - (void)hideTransparentNavigationBar
    {
        [self.navigationController.navigationBar setHidden:YES];
        [self.navigationController.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]];
        [self.navigationController.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]];
    }
    
    - (void) showMenu:(id) sender{ /*TODO when clicks on Menu button */ }
    - (void) popViewController{    /*TODO when clicks on Back button */ }
    
    @end
    

    如果管理得当,当您单击屏幕上的显示按钮时,您应该能够将菜单和返回按钮转到导航栏。

    注意:

    您可以在 AppDelegate 文件中执行此操作,以便在整个应用程序中使用它

    【讨论】:

    • 这是一个很好的答案!但是如何在每个 ViewController 上使用相同的菜单按钮而不在每个视图中复制代码?
    • 谢谢!!您需要在 AppDelegate 的方法 didFinishLaunchingWithOptions 中使用相同的逻辑
    • 可以在appdelegate中不定义rootviewController的情况下初始化UINavigationViewController吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多