ios - 在搜索栏的左侧和右侧添加按钮

标签 ios search uisearchbar uisearchcontroller searchbar

我想要一个搜索栏,它在文本字段的左侧和右侧都有一个按钮。与当前 Alien Blue reddit 应用程序中的搜索栏相同。

AlienBlue SearchBar

到目前为止,我最接近实现这一目标的是我已经能够使用这行代码更改右侧“取消”按钮的文本。

[self.searchController.searchBar setValue:@"Hello" forKey:@"_cancelButtonText"];

但这并没有真正帮助,因为我希望右侧打开不同的搜索选项,而不是取消搜索。

帮助我____,你是我唯一的希望

最佳答案

先说效果

enter image description here

//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/

相关文章:

ios - 不处理从这里抛出的错误

python - 用 Python 在给定目录及其子目录中递归替换文件中的字符串?

MySQL 转义一切(所有特殊字符)

iphone - UISearchBar - 范围栏动画

ios - 消除键盘 FirstResponder 问题

ios - Alamofire 附加数据

ios - Admob导致链接器命令失败,退出代码为1错误

ios - 如何根据通过 TableView 进行的选择在 uiview 中显示不同的 View Controller (请参阅说明中的图片)?

javascript - 在 Accordion 中搜索

swift - 加载 Controller 时激活 UISearchBar