苹果自带的UISearchBar还是蛮好用的,但是在使用过程中也遇到一些坑,比如如何找到searchBar右侧的取消button,以及第一次点击的时候会执行UISearchBar的代理方法:searchBarTextDidBeginEditing的问题。首先如何获取cancelButton并且进行自定义呢?for(UIViewviewin[[_searchBarsubviewslastObject]subviews]){if([viewisKindOfClass:[UIButtonclass]]){UIButtoncancelBtn=(UIButton)view;cancelBtnenabled=YES;[cancelBtnsetTitle:@"取消"forState:UIControlStateNormal];[cancelBtnsetTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];[cancelBtnaddTarget:selfaction:@selector(cancelButtonClicked:)forControlEvents:UIControlEventTouchUpInside];}}上面这是复杂一点的方式,下面用更简单的方式获取,那就是利用kvo原理:UIButtoncancelBtn=[_searchBarvalueForKeyPath:@"cancelButton"];cancelBtnenabled=YES;[cancelBtnsetTitle:@"取消"forState:UIControlStateNormal];[cancelBtnsetTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];[cancelBtnaddTarget:selfaction:@selector(cancelButtonClicked:)forControlEvents:UIControlEventTouchUpInside];大家可能注意到,其中有一行代码cancelBtnenabled=YES;为什么要写这句代码呢?是因为searchBar加载出来以后右侧的取消按钮是默认enabled=NO,所以才会出现点击的时候执行searchBarTextDidBeginEditing方法。注意一点,在搜索完成,searchBar失去焦点的地方,也需要获取到cancelBtn并且设置cancelBtnenabled=YES;比如在点击键盘上的搜索按钮://键盘上搜索按钮被点击-(void)searchBarSearchButtonClicked:(UISearchBar)searchBar{[searchBarresignFirstResponder];UIButtoncancelBtn=[searchBarvalueForKeyPath:@"cancelButton"];//首先取出cancelBtncancelBtnenabled=YES;}这样点击取消按钮就会直接退出搜索页面,而不是执行searchBarTextDidBeginEditing方法。
layercornerRadius =buttonwidth/2
btnframe = CGRectMake(x, y, width, height);
[btn setTitle: @"search" forState: UIControlStateNormal];
//[btn setFont: [UIFont systemFontSize: 140]]; //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法
btntitleLabelfont = [UIFont systemFontOfSize: 140];
[btn seBackgroundColor: [UIColor blueColor]];
[superView addSubview: btn];
==========================================================
tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];
btncontentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;
[btn setTitle:@“title”forState:UIControlStateNormal];
有些时候我们想让UIButton的title居左对齐,我们设置
btntextLabeltextAlignment = UITextAlignmentLeft
是没有作用的,我们需要设置
btncontentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;
但是问题又出来,此时文字会紧贴到做边框,我们可以设置
btncontentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距离做边框保持10个像素的距离。
=======================================================
设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:
[btntitleLabel setTextColor:[UIColorblackColor]];
btntitleLabeltextColor=[UIColor redColor];
而是用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
selected
enabled
// buttontitleLabeltextAlignment = NSTextAlignmentLeft; 这句无效
buttoncontentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
buttontitleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
这里使用
buttontitleLabeltextAlignment = NSTextAlignmentLeft; 这行代码是没有效果的,这只是让标签中的文本左对齐,但
并没有改变标签在按钮中的对齐方式。
所以,我们首先要使用
buttoncontentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 这行代码,把按钮的内容(控件)
的对齐方式修改为水平左对齐,但是这们会紧紧靠着左边,不好看,
所以我们还可以修改属性:
buttontitleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
这行代码可以让按钮的内容(控件)距离左边10个像素,这样就好看多了
以上就是关于UISearchBar点击取消按钮,不触发searchBarCancelButtonClicked函数的问题全部的内容,包括:UISearchBar点击取消按钮,不触发searchBarCancelButtonClicked函数的问题、button(selected、enabled、……)相关内容、怎么修改 UIButton 的文字等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)