2

此代码在 iOS 6 中运行良好,但在 iOS 7 中,导航栏中的文本字段是灰色的,并且不可点击?看看这张图的区别

在此处输入图像描述

可能有什么问题?我不知道它们在 iOS 7 中到底发生了什么变化,也不知道从哪里开始寻找解决这个问题的方法......

/问候

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

1 回答 1

3

在 ios7[searchBar.subviews lastObject]不是文本字段,而是UIView作为控件周围的附加容器的实例。

与鸡尾酒有同样的问题,我计划在以下类别中使用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

- subcontrols方法在这里起到了作用。

于 2013-10-08T19:05:12.010 回答