简体   繁体   中英

UITextfield not working as subview to UISearchBar in iOS 7?

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];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM