UITextfield not working as subview to UISearchBar in iOS 7?

自作多情 提交于 2019-12-02 06:12:45

问题


This code worked perfectly fine in iOS 6 but in iOS 7 the textfield is gray in the navigationBar, and not clickable? See the difference in this picture

What may be wrong ? I am not aware of what they have changed exactly in iOS 7 and not sure where to start looking for solving this problem...

/Regards

UITextField *sbTextField = (UITextField *)[searchBar.subviews lastObject];
[sbTextField removeFromSuperview];

CGRect rect = searchBar.frame;
rect.size.height = 32;
rect.size.width = 210;
sbTextField.frame = rect;
 // [sbTextField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation]; Not working in iOS7
 // [sbTextField setPlaceholder:NSLocalizedString(@"HintSearchExercise", nil)]; Not working in iOS 7

[sbTextField setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
[searchBar removeFromSuperview];

UIBarButtonItem *searchBarNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:sbTextField];

[[self navigationItem] setLeftBarButtonItem:searchBarNavigationItem];

回答1:


in ios7 [searchBar.subviews lastObject] is not the text field but instead a UIView instance that acts as an additional container around the controls.

having the same problem with cocktailicious, i plan to use the following category on UISearchBar:

@interface UISearchBar (Workarounds)
@property (readonly, nonatomic) UITextField *textField;
@end

@implementation UISearchBar (Workarounds)
- (UITextField *)textField
{
    for (UIView *view in [self subcontrols]) {
        if ([view isKindOfClass:[UITextField class]]) {
            return (UITextField *)view;
        }
    }
    return nil;
}

- (NSArray *)subcontrols
{
    return self.subviews.count == 1 ? [self.subviews.firstObject subviews] : self.subviews;
}
@end

the - subcontrols method does the trick here.



来源:https://stackoverflow.com/questions/18993773/uitextfield-not-working-as-subview-to-uisearchbar-in-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!