我想要实现的是将UISearchBar
的放大镜图标从文本字段的左边缘移动到右边缘。这就是我这样做的方式:
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FRSearchMaxHeight)];
searchBar.delegate = self;
searchBar.placeholder = NSLocalizedString(@"Search friends", "");
searchBar.showsCancelButton = NO;
UIView *view = [searchBar subviewConformingToProtocol:@protocol(UITextInput)];
UIView<UITextInput> *textInputView = (UIView<UITextInput> *)view;
[textInputView setReturnKeyType:UIReturnKeyDone];
[textInputView setKeyboardAppearance:UIKeyboardAppearanceAlert];
tableView.tableHeaderView = searchBar;
UIImage *image = [UIImage imageWithColor:[UIColor colorWithRed:0.9f green:0.9f blue:0.9f alpha:0.9f] size:searchBar.frame.size];
[searchBar setBackgroundImage:image];
textInputView.layer.cornerRadius = 0.f;
textInputView.layer.borderWidth = 1.f;
textInputView.layer.borderColor = [UIColor lightGrayColor].CGColor;
UIView *magnifier = [(UITextField *)textInputView leftView];
[(UITextField *)textInputView setRightView:magnifier];
[(UITextField *)textInputView setLeftViewMode:UITextFieldViewModeNever];
[(UITextField *)textInputView setRightViewMode:UITextFieldViewModeAlways];
然而,放大镜从未显示过。它消失了。我检查了这样的框架:
NSLog(@"FRAME %@", NSStringFromCGRect(((UITextField *)textInputView).rightView.frame));
它说:FRAME {{0, 0}, {12.5, 12.5}}
。我错过了什么?