UISearchBar的文本字段。右视没有显示

时间:2014-05-14 13:41:02

标签: ios objective-c uikit uisearchbar

我想要实现的是将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}}。我错过了什么?

0 个答案:

没有答案