[英]UITextfield not working as subview to UISearchBar in iOS 7?
这段代码在iOS 6中工作得很好,但是在iOS 7中,navigationBar中的文本字段为灰色,并且无法单击? 看到这张图片的区别
可能是什么问题? 我不知道它们在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];
在ios7中, [searchBar.subviews lastObject]
不是文本字段,而是一个UIView
实例,它充当控件周围的附加容器。
与cocktailicious有相同的问题,我计划在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
方法在这里起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.