[iOS]导航条自定义及自适应

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"首页";
    NSLog(@"---%f",self.navigationItem.titleView.frame.size.width);
    
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = [UIColor lightGrayColor];
    titleView.translatesAutoresizingMaskIntoConstraints = NO;
    self.navigationItem.titleView = titleView;
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(2);
        make.left.mas_offset(5);
        make.right.mas_offset(-5);
        make.bottom.mas_offset(-5);
    }];
    
    
    UIButton *chooseCity = [UIButton buttonWithType:UIButtonTypeCustom];
    chooseCity.backgroundColor = [UIColor clearColor];
    [chooseCity setTitle:@"郑州" forState:UIControlStateNormal];
    [chooseCity setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [titleView addSubview:chooseCity];
    [chooseCity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(5);
        make.left.mas_offset(5);
        make.bottom.mas_offset(-5);
        make.width.mas_equalTo(60);
    }];
    
    UITextField *textField = [[UITextField alloc] init];
    textField.placeholder = @"请输入搜索的关键字";
    textField.backgroundColor = [UIColor lightGrayColor];
    [titleView addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(chooseCity.mas_right).mas_offset(0);
        make.top.mas_offset(5);
        make.bottom.mas_offset(-5);
        make.width.mas_offset(200);
    }];
    
    
    
    [self initTableView];

}

[iOS]导航条自定义及自适应_第1张图片
IMG_2014.PNG
[iOS]导航条自定义及自适应_第2张图片
IMG_2015.PNG

你可能感兴趣的:([iOS]导航条自定义及自适应)