UITextfield不能作为iOS 7中UISearchBar的子视图? [英] UITextfield not working as subview to UISearchBar in iOS 7?

查看:123
本文介绍了UITextfield不能作为iOS 7中UISearchBar的子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

可能出现什么问题?我不知道他们在iOS 7中究竟有什么变化而且不知道从哪里开始寻找解决这个问题...

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...

/问候

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。子视图lastObject] 文本字段,而是 UIView 实例,它充当控件周围的附加容器。

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

cocktailicious 有同样的问题,我打算在 UISearchBar上使用以下类别

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

- 子控制方法可以解决问题。

这篇关于UITextfield不能作为iOS 7中UISearchBar的子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆