Custom InputView for UISearchBar doesn't work in iOS7

浪子不回头ぞ 提交于 2019-12-12 07:20:06

问题


I need to set my custom inputView in UISearchBar.

So i wrote following codes , that is working perfectly in iOS5 and iOS6 .

However it doesn't work in iOS7.

for(int i =0; i<[self.searchBar.subviews count]; i++) {
            if([[self.searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]])
            {
                [(UITextField*)[searchBar.subviews objectAtIndex:i] setFont:[UIFont fontWithName:@"MyCustomFont" size:15]];
                UITextField* search=(UITextField*)[searchBar.subviews objectAtIndex:i];
                search.delegate = self;

                [(UITextField*)[self.searchBar.subviews objectAtIndex:i] setInputView:self.customKeyboard];
                [self.customKeyboard setTextView:search];
            }
        }



        [self.sBar reloadInputViews];

How can i do that?


回答1:


The searchBar sub views hierarchy has been changed in iOS7, try the below:

iOS7:

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];

iOS6 and before:

NSArray *searchBarSubViews =  self.searchBar.subviews;

Try below updated function:

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for(int i =0; i<[searchBarSubViews count]; i++) {
    if([[searchBarSubViews objectAtIndex:i] isKindOfClass:[UITextField class]])
    {
        UITextField* search=(UITextField*)[searchBarSubViews objectAtIndex:i];
        [search setFont:[UIFont fontWithName:@"MyCustomFont" size:15]];
        search.delegate = self;

        [search setInputView:self.customKeyboard];
        [self.customKeyboard setTextView:search];
    }
}
[self.sBar reloadInputViews];


来源:https://stackoverflow.com/questions/18972938/custom-inputview-for-uisearchbar-doesnt-work-in-ios7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!