zoukankan      html  css  js  c++  java
  • 设置导航条按钮

    #import "XMGEssenceViewController.h"
    
    // UIBarButtonItem:描述按钮具体的内容
    // UINavigationItem:设置导航条上内容(左边,右边,中间)
    // tabBarItem: 设置tabBar上按钮内容(tabBarButton)
    @interface XMGEssenceViewController ()
    
    @end
    
    @implementation XMGEssenceViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor redColor];
        
        // 设置导航条
        [self setupNavBar];
    }
    
    #pragma mark - 设置导航条
    - (void)setupNavBar
    {
        
        // 左边按钮
        // 把UIButton包装成UIBarButtonItem.就导致按钮点击区域扩大
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithimage:[UIImage imageNamed:@"nav_item_game_icon"] highImage:[UIImage imageNamed:@"nav_item_game_click_icon"] target:self action:@selector(game)];
        
        // 右边按钮
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithimage:[UIImage imageNamed:@"navigationButtonRandom"] highImage:[UIImage imageNamed:@"navigationButtonRandomClick"] target:nil action:nil];
        
        // titleView
        self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    }
    
    - (void)game
    {
        XMGFunc;
    }
    
    @end

    UIBarButtonItem+Item.h

    #import <UIKit/UIKit.h>
    
    @interface UIBarButtonItem (Item)
    // 快速创建UIBarButtonItem
    + (UIBarButtonItem *)itemWithimage:(UIImage *)image highImage:(UIImage *)highImage target:(id)target action:(SEL)action;
    
    + (UIBarButtonItem *)itemWithimage:(UIImage *)image selImage:(UIImage *)selImage target:(id)target action:(SEL)action;
    
    @end

    UIBarButtonItem+Item.m

    #import "UIBarButtonItem+Item.h"
    
    @implementation UIBarButtonItem (Item)
    + (UIBarButtonItem *)itemWithimage:(UIImage *)image highImage:(UIImage *)highImage target:(id)target action:(SEL)action
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:image forState:UIControlStateNormal];
        [btn setImage:highImage forState:UIControlStateHighlighted];
        [btn sizeToFit];
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        UIView *containView = [[UIView alloc] initWithFrame:btn.bounds];
        [containView addSubview:btn];
        
        return [[UIBarButtonItem alloc] initWithCustomView:containView];
    }
    
    + (UIBarButtonItem *)itemWithimage:(UIImage *)image selImage:(UIImage *)selImage target:(id)target action:(SEL)action
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:image forState:UIControlStateNormal];
        [btn setImage:selImage forState:UIControlStateSelected];
        [btn sizeToFit];
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        UIView *containView = [[UIView alloc] initWithFrame:btn.bounds];
        [containView addSubview:btn];
        
        return [[UIBarButtonItem alloc] initWithCustomView:containView];
    }
    
    
    @end
    #import "XMGMeViewController.h"
    
    @interface XMGMeViewController ()
    
    @end
    
    @implementation XMGMeViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 设置导航条
        [self setupNavBar];
        
    }
    
    - (void)setupNavBar
    {
        // 左边按钮
        // 把UIButton包装成UIBarButtonItem.就导致按钮点击区域扩大
        
        // 设置
        UIBarButtonItem *settingItem =  [UIBarButtonItem itemWithimage:[UIImage imageNamed:@"mine-setting-icon"] highImage:[UIImage imageNamed:@"mine-setting-icon-click"] target:self action:@selector(setting)];
    
        // 夜间模型
        UIBarButtonItem *nightItem =  [UIBarButtonItem itemWithimage:[UIImage imageNamed:@"mine-moon-icon"] selImage:[UIImage imageNamed:@"mine-moon-icon-click"] target:self action:@selector(night:)];
    
        self.navigationItem.rightBarButtonItems = @[settingItem,nightItem];
        
        // titleView
        self.navigationItem.title = @"我的";
        
    }
    
    - (void)night:(UIButton *)button
    {
        button.selected = !button.selected;
        
    }
  • 相关阅读:
    BZOJ2821 作诗(Poetize) 【分块】
    BZOJ2724 蒲公英 【分块】
    Codeforces 17E Palisection 【Manacher】
    BZOJ2565 最长双回文串 【Manacher】
    Codeforces 25E Test 【Hash】
    CODEVS3013 单词背诵 【Hash】【MAP】
    HDU2825 Wireless Password 【AC自动机】【状压DP】
    HDU2896 病毒侵袭 【AC自动机】
    HDU3065 病毒侵袭持续中【AC自动机】
    HDU2222 Keywords Search 【AC自动机】
  • 原文地址:https://www.cnblogs.com/xufengyuan/p/6501480.html
Copyright © 2011-2022 走看看