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

2021-03-04  本文已影响0人  汴城码农
- (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];

}

IMG_2014.PNG IMG_2015.PNG
上一篇 下一篇

猜你喜欢

热点阅读