UI改制 - UISearchBar居左显示
UISearchBar居左显示。
1 - 示例代码:创建 UINoMidSearchBar 类,继承于 UISearchBar
UINoMidSearchBar.h------------------------------------------------------------------
1 #import <UIKit/UIKit.h> 2 @interface UINoMidSearchBar : UISearchBar 3 4 @property (nonatomic, assign, setter = setHasCentredPlaceholder:) BOOL hasCentredPlaceholder; 5 6 7 @end
UINoMidSearchBar.m------------------------------------------------------------------
1 #import "UINoMidSearchBar.h" 2 @implementation UINoMidSearchBar 3 4 - (instancetype)initWithFrame:(CGRect)frame{ 5 6 self = [super initWithFrame:frame]; 7 if (self) { 8 self.hasCentredPlaceholder = YES; 9 } 10 return self; 11 } 12 13 -(void)setHasCentredPlaceholder:(BOOL)hasCentredPlaceholder{ 14 15 _hasCentredPlaceholder = hasCentredPlaceholder; 16 SEL centerSelector = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"setCenter", @"Placeholder:"]); 17 if ([self respondsToSelector:centerSelector]){ 18 19 NSMethodSignature *signature = [[UISearchBar class] instanceMethodSignatureForSelector:centerSelector]; 20 21 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; 22 [invocation setTarget:self]; 23 [invocation setSelector:centerSelector]; 24 [invocation setArgument:&_hasCentredPlaceholder atIndex:2]; 25 [invocation invoke]; 26 } 27 } 28 29 @end
2 - 测试
1 #import "ViewController.h" 2 #import "UINoMidSearchBar.h" 3 @interface ViewController () 4 5 @property(strong,nonatomic)UINoMidSearchBar *testSearchBar;//搜索框 6 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 self.view.backgroundColor = [UIColor whiteColor]; 14 15 //创建搜索框 16 self.testSearchBar = [[UINoMidSearchBar alloc] initWithFrame:CGRectMake(0, 200, 320, 50)]; 17 self.testSearchBar.placeholder = @"窈窕淑女"; 18 self.testSearchBar.hasCentredPlaceholder = NO;// 占位字符默认非居中 19 //[self.testSearchBar setImage:[UIImage imageNamed:@""] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];// 修改搜索图片(放大镜) 20 //[[[self.testSearchBar.subviews objectAtIndex:0].subviews objectAtIndex:1] setTintColor:[UIColor greenColor]];// 设置光标颜色 21 //self.testSearchBar.searchBarStyle = UISearchBarStyleMinimal;// 祛除边框 22 [self.testSearchBar setKeyboardType:UIKeyboardTypeEmailAddress]; 23 [self.view addSubview:_testSearchBar]; 24 25 } 26 27 28 @end
运行效果:
优质内容筛选与推荐>>