开发者

How to do a custom UISearchBar for iPhone

based on UISearchBar I want to the following features

  1. NO border, origin searchbar has round corner textfield. this fragment code can make it no border, *but there is a problem: if set the bookmark button showed, the bookmark button border seems be cut 1 pixel too *

     UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10, 5, 300, 45)];
    
     UITextField *inputField = (UITextField *)[textField.subviews objectAtIndex:0];          
     inputField.backgroundColor = [UIColor clearColor];
    
     inputField.layer.borderWidth = 5.0f;
     inputField.layer.cornerRadius = 14;
    开发者_如何学Python inputField.layer.borderColor = [[UIColor whiteColor] CGColor];
    
  2. custom bookmark button icon

  3. placeholder textColor I have no idea about this. anybody has clues?


As of iOS 5 you've got the brilliant UIAppearance proxy that will let you set all that stuff with one line of code, but if you want to target 4 or below, then you're rolling your own - you're going to have to create your own custom view class, OR, do some subview hacking. You've already started in that first snippet, but there's a problem...

The "proper" way to do subview hacking is to iterate over the subviews array until you find a view that matches the one you're looking for. In this case, what you want to do is something like:

for(UIView* i in [searchBar subviews) ]{
    if ([i isKindOfClass:[UITextField class]]) {
        //success! do what you want to do and break
        break;
    }
}
//maybe put some code here if you didn't find what you were looking for

The problem with subview hacking is that the view hierarchy can change at any time, without warning, and suddenly (at best) your app doesn't look the same or (at worst) it crashes. For this reason I'd recommend rolling your own or just targeting iOS 5 where this stuff is all really simple.


As of iOS 7, the solution proposed by Morgan Harris and wagashi does not work anymore, as the view hierarchy has changed (Morgan Harris already warned us about that in his answer). The condition will therefore not be hit anymore.

If you still need to change the appearance by subview hacking, here is an updated piece of code which will work for iOS 7:

// Make sure the searchbar has subviews
if (searchBar.subviews && searchBar.subviews.count > 0) {
    // Get main searchBar view
    UIView *searchBarView = [searchBar.subviews objectAtIndex:0];
    // Iterate through its subviews
    for (UIView* searchBarSubview in [searchBarView subviews]) {
        // Check for a text field
        if ([searchBarSubview isKindOfClass:[UITextField class]]) {
            // Success. Now you can change its appearance.
            break;
        }
    }
}

Please note that you need to check the iOS version beforehand, if you target earlier versions as well.

If you can, you should probably use UIAppearance, as proposed by Morgan Harris. For your convenience, here is the developer library reference. There was also a nice blog post about UIAppearance by Mattt Thompson.


NSArray *subviews = [mySearchBar subviews] ;
    for(id subview in subviews) {
        if([subview isKindOfClass:[UITextField class]]) {
            [(UITextField*)subview setReturnKeyType:UIReturnKeyDone];
            [(UITextField*)subview setFrame:CGRectMake(0, 0, 100, 40)];
        }
    }

Return Key Done does work, but not frame of textfield. Why? By the way, my search Bars frame is set to 0, 0 , 320, 40.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜