ios自定義簡約導航欄

weixin_34054866發表於2018-02-07

功能描述:提供導航標題,左右按鈕及點選功能。

做法

新建一個類,繼承自UIView。
在.h檔案中提供外界建立方法,標題顏色屬性

@interface PDtopBar : UIView

@property(nonatomic,strong)UIColor *titleColor;
@property(nonatomic,strong)UIColor *viewColor;   // 背景色
@property(nonatomic,assign)BOOL hidden;   //隱藏?

@property(nonatomic,strong)void(^gobackBlock)();   // 返回上一頁block
@property(nonatomic,strong)void(^clickRightBtnBlock)();    // 點選右按鈕block

// 提供外界建立方法
-(instancetype)initWithTitle:(NSString *)title rightBtnTitle:(NSString *)rightText;

@end

.m檔案中,宣告子控制元件

@interface PDtopBar()

@property(nonatomic,weak)UILabel *topTitleLabel;   // 標題

@property(nonatomic,weak)UIButton *backBtn;   // 左邊的返回按鈕

@property(nonatomic,weak)UIButton *rightBtn;   // 右邊的按鈕

@end

實現建立方法

-(instancetype)initWithTitle:(NSString *)title rightBtnTitle:(NSString *)rightText{
    
    if(self = [super init]){
        self.backgroundColor = [UIColor whiteColor];
        
        //位置,尺寸
        self.frame = CGRectMake(0, 20, screenW, 44);
        if(screenH == 812)
            self.frame = CGRectMake(0, 44, screenW, 44);    // 適配IphoneX
        
        UILabel *toplabel = [[UILabel alloc] Label_color3:Font18 text:title];
        toplabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:toplabel];
        _topTitleLabel = toplabel;
        [_topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.centerX.equalTo(self.mas_centerX);
            make.width.equalTo(self.mas_width).multipliedBy(0.6);
        }];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
        [btn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
        [self addSubview:btn];
        _backBtn = btn;
        [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(5);
            make.centerY.equalTo(_topTitleLabel.mas_centerY);
            make.height.equalTo(@20);
            make.width.equalTo(@50);
        }];
        
    if(rightText){
            UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
            btn2.titleLabel.font = Font16;
            [btn2 setTitleColor:Color333333 forState:UIControlStateNormal];
            [btn2 addTarget:self action:@selector(clickRight) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn2];
            _rightBtn = btn2;
            [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.mas_right).offset(-5);
                make.centerY.equalTo(_topTitleLabel.mas_centerY);
                make.height.equalTo(@20);
                make.width.equalTo(@50);
            }];
       }
    }
    return self;
}

點選事件處理

-(void)clickRight{
      if(self.clickRightBtnBlock)
          self.clickRightBtnBlock();
}

-(void)clickBack{
       if(self.gobackBlock)
          self.gobackBlock();
}

屬性樣式設定
標題顏色

-(void)setTitleColor:(UIColor *)titleColor{
    self.topTitleLabel.textColor =titleColor;
}

背景色

-(void)setViewColor:(UIColor *)viewColor{
    self.backgroundColor = viewColor;
}

隱藏屬性

-(void)setHidden:(BOOL)hidden{
    self.hidden = hidden;
}

外界使用

建立時提供標題、右標題、左右按鈕的程式碼block即可。

總結:比較簡陋,只有基本的常用功能。請按需使用。

相關文章