Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Customizing search bar in iOS SDK

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 335
    Comment on it

    Hi Readers,

    Customizing search bar can be easily done with UISearchBar. All you need to check view hierarchy of search bar subviews for that as with latest iOS SDK hierarchy can get changed. Customizing can be done by changing these views color, font, background color, icon etc. For now let say we need to clear background of search bar and change font style and size of cancel button. Following code snippet can help you out with that:

    for(UIView *view in [searchBar subviews]) {
            
                for(UIView *view1 in [view subviews]) {
                    
                    if([view1 isKindOfClass:[NSClassFromString(@"UISearchBarBackground") class]]) {
    
                        view1.alpha = 0.0;
                        
                    }
    
                    
                    if([view1 isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
                        
                        UIButton *button = (UIButton *)view1;
                        
                        button.titleLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightHeavy];
        
                }
            }
        }
    

    Here you can see that we enumerate all subviews of search bar until we get UISearchBarBackground class object and UINavigationButton class object. With background view class we put alpha to 0, by which background color gets clear. In second condition for UINavigationButton, we get cancel button object, by which we can set its font, text color and title text also.

    So which this approach you can easily customize your search bar according to requirement. Hope it helps.

    Thanks for reading.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: