我想要一个搜索栏,它在文本字段的左侧和右侧都有一个按钮。与当前 Alien Blue reddit 应用程序中的搜索栏相同。
到目前为止,我最接近实现这一目标的是我已经能够使用这行代码更改右侧“取消”按钮的文本。
[self.searchController.searchBar setValue:@"Hello" forKey:@"_cancelButtonText"];
但这并没有真正帮助,因为我希望右侧打开不同的搜索选项,而不是取消搜索。
帮助我____,你是我唯一的希望
最佳答案
先说效果
//RWSearchBar.h
#define BarFrame CGRectMake(100, 20, 200, 44)
#import <UIKit/UIKit.h>
@interface RWSearchBar : UISearchBar
@end
//RWSearchBar.m
#import "RWSearchBar.h"
@implementation RWSearchBar
- (void)layoutSubviews {
self.frame = BarFrame;
}
@end
//RWViewController.m
#import "ViewController.h"
#import "RWSearchBar.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
RWSearchBar *searchBar = [[RWSearchBar alloc] init];
[self.view addSubview:searchBar];
CGRect aFrame = BarFrame;
UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(aFrame.origin.x - 60, aFrame.origin.y, 60, 44)];
leftBtn.backgroundColor = [UIColor blueColor];
UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(aFrame.origin.x + aFrame.size.width, aFrame.origin.y, 60, 44)];
rightBtn.backgroundColor = [UIColor yellowColor];
[self.view addSubview:leftBtn];
[self.view addSubview:rightBtn];
}
您可以自定义更多的视觉效果。
关于ios - 在搜索栏的左侧和右侧添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33539140/