上图!
顶部的输入框和按钮下部的分割线,具体实现代码如下:
- (void)setUpNav{
//设置放大镜
UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"组-15"]];
//设置输入框
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(5, 5, 260, 40)];
self.searchTextField = textField;
//放大镜放入输入框中
textField.rightView = img;
textField.rightViewMode = UITextFieldViewModeAlways;
textField.backgroundColor = [UIColor whiteColor];
textField.placeholder = @"输入医院名称查找";
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.layer.cornerRadius = 5;
self.navigationItem.titleView = textField;
}
//添加按钮
- (UIView *)setUpHeaderView {
UIView * header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 60)];
//左按钮
UIButton *leftBtn = [[UIButton alloc]init];
//文字
[leftBtn setTitle:@"医院等级" forState:UIControlStateNormal];
//颜色
[leftBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
//小箭头
[leftBtn setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 160, 0, 0);
[header addSubview:leftBtn];
//右按钮
UIButton *rightBtn = [[UIButton alloc]init];
[rightBtn setTitle:@"医院地区" forState:UIControlStateNormal];
[rightBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[rightBtn setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 160, 0, 0);
//分割线
UIView *line = [[UIView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2 , 10, 1, 30)];
line.backgroundColor = [UIColor grayColor];
UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, 1)];
line2.backgroundColor = [UIColor grayColor];
[header addSubview:rightBtn];
[header addSubview:line];
[header addSubview:line2];
//添加约束
[leftBtn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(header.mas_top);
make.left.equalTo(header.mas_left);
make.bottom.equalTo(header.mas_bottom);
make.right.equalTo(header.mas_left).offset([UIScreen mainScreen].bounds.size.width/2);
}];
[rightBtn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(header.mas_top);
make.right.equalTo(header.mas_right);
make.bottom.equalTo(header.mas_bottom);
make.left.equalTo(header.mas_left).offset([UIScreen mainScreen].bounds.size.width/2);
}];
[leftBtn addTarget:self action:@selector(BtnClick) forControlEvents:UIControlEventTouchUpInside];
return header;
}