English 中文(简体)
iPhone UISearchBar & keyboardAppearance
原标题:

When the keyboard appears, I want to set the

keyboardAppearance = UIKeyboardAppearanceAlert

I ve checked the documentation and it looks like you can only change the keyboardType.

Can this be done without violating any of Apple s private API s ?

问题回答

The best way that I found to do this, if you want to do it throughout your app, is to use Appearance on UITextField. Put this in your AppDelegate on launch.

[[UITextField appearance] setKeyboardAppearance:UIKeyboardAppearanceDark];

This should do it:

for(UIView *subView in searchBar.subviews)
    if([subView isKindOfClass: [UITextField class]])
        [(UITextField *)subView setKeyboardAppearance: UIKeyboardAppearanceAlert];

didn t find any other way of doing...

This no longer works on iOS 7 because the UISearchBar view hierarchy has changed. The UITextView is now a subview of the first subview (e.g. its in the searchBar.subviews[0].subviews array).

A more future proof way to do this would be to check recursively the entire view hierarchy, and to check for UITextInputTraits protocol rather than UITextField, since that is what actually declares the method. A clean way of doing this is to use categories. First make a category on UISearchBar that adds this method:

- (void) setKeyboardAppearence: (UIKeyboardAppearance) appearence {
    [(id<UITextInputTraits>) [self firstSubviewConformingToProtocol: @protocol(UITextInputTraits)] setKeyboardAppearance: appearence];
}

Then add a category on UIView that adds this method:

- (UIView *) firstSubviewConformingToProtocol: (Protocol *) pro {
    for (UIView *sub in self.subviews)
        if ([sub conformsToProtocol: pro])
            return sub;

    for (UIView *sub in self.subviews) {
        UIView *ret = [sub firstSubviewConformingToProtocol: pro];
        if (ret)
            return ret;
    }

    return nil;
}

You can now set the keyboard appearance on the search bar in the same way you would a textfield:

[searchBar setKeyboardAppearence: UIKeyboardAppearanceDark];

keyboardAppearance is a property of the UITextInputTraitsProtocol, which means that the property is set via the TextField object. I m not aware of what an Alert Keyboard is, from the SDK it s a keyboard suitable for an alert.

here s how you access the property:

UITextField *myTextField = [[UITextField alloc] init];
myTextField.keyboardAppearance = UIKeyboardAppearanceAlert;

Now when the user taps the text field and the keyboard shows up, it should be what you want.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签