美團自定義導航欄思路以及代碼

一.右邊兩個圖標:直接添加兩個UIBarButtonItem,其中各自添加UIButton,而後再設置button的圖片,爲了方便,使用分類自定義,UIBarButtonItem+DQCategory.hide

二.左邊加四個UIBarButtonItem,其中各自添加自定義的view,設置好xib的寬高,連個lable,一個按鈕button,按鈕應該覆蓋整個區域以確保整個xib點擊都有效果,而後設置按鈕靠左對其便可atom

1.右邊圖標按鈕spa

分類.h文件

+ (UIBarButtonItem *)itemWithImageName:(NSString *)imageName highImageName:(NSString *)highImageName taget:(id)taget action:(SEL)action3d

 分類.m文件 //只能添加方法,不能添加屬性
+ (UIBarButtonItem *)itemWithImageName:(NSString *)imageName highImageName:(NSString *)highImageName taget:(id)taget action:(SEL)action{ UIButton *button = [[UIButton alloc] init]; [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];//普通
    [button setImage:[UIImage imageNamed:highImageName] forState:UIControlStateHighlighted];//高亮
    [button addTarget:taget action:action forControlEvents:UIControlEventTouchUpInside];//點擊事件
    button.frame = CGRectMake(0, 0, 60, 40);//根據寬度調整兩個圖標的間距 // button.backgroundColor = [UIColor redColor];
    return [[UIBarButtonItem alloc]initWithCustomView:button]; }
//在控制器.m w文件中 設置右邊兩個items
- (void)setUpRightNavItems{ //地圖
    UIBarButtonItem *mapItem = [UIBarButtonItem itemWithImageName:@"icon_map" highImageName:@"icon_map_highlighted" taget:self action:@selector(clickMap)]; //搜索框
    UIBarButtonItem *searchItem = [UIBarButtonItem itemWithImageName:@"icon_search" highImageName:@"icon_search_highlighted" taget:self action:@selector(clickSearch)]; self.navigationItem.rightBarButtonItems = @[mapItem,searchItem]; }

2.左邊的區域code

.xib(140*40)   DQNavLeftView.horm

按鈕設置(約束都是0)blog

DQNavLeftView.h @property (weak, nonatomic) IBOutlet UILabel *subTitleLable; @property (weak, nonatomic) IBOutlet UILabel *titleLable; @property (weak, nonatomic) IBOutlet UIButton *imageButton; //返回從xib加載的視圖
+ (id)view;

DQNavLeftView.m

//返回從xib加載的視圖排序

+ (id)view{事件

    return [[[NSBundle mainBundle] loadNibNamed:@"DQNavLeftView" owner:nil options:nil] lastObject];圖片

}

 

//防止控件拉伸

- (id)initWithCoder:(NSCoder *)aDecoder{

    if (self=[super initWithCoder:aDecoder]) {

        self.autoresizingMask = UIViewAutoresizingNone;

    }

    return self;

}

 

控制器.m文件 

//設置左邊items

- (void)setUpLeftNavItems{

    //LOGO

    UIBarButtonItem *logoItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_meituan_logo"] style:UIBarButtonItemStyleDone target:nil action:nil];

    logoItem.enabled = NO;

    

    //分類

    self.categoryView = [DQNavLeftView view];

    UIBarButtonItem *categoryItem = [[UIBarButtonItem alloc]initWithCustomView:self.categoryView];

    [self.categoryView.imageButton addTarget:self action:@selector(cilckCategoryView) forControlEvents:UIControlEventTouchUpInside];

    

    //城市

    self.cityView = [DQNavLeftView view];

    self.cityView.titleLable.text = @"廣州";//首頁加載完一開始顯示的

    UIBarButtonItem *cityItem = [[UIBarButtonItem alloc]initWithCustomView:self.cityView];

    [self.cityView.imageButton addTarget:self action:@selector(clickCityView) forControlEvents:UIControlEventTouchUpInside];

    

    //排序

    self.sortView = [DQNavLeftView view];

    self.sortView.titleLable.text = @"默認排序";//首頁加載完一開始顯示的

    [self.sortView.imageButton addTarget:self action:@selector(clickSortView) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *sortItem = [[UIBarButtonItem alloc]initWithCustomView:self.sortView];

    

    

    self.navigationItem.leftBarButtonItems = @[logoItem,categoryItem,cityItem,sortItem];

}

相關文章
相關標籤/搜索