UITextField within UISearchBar in iOS 7

后端 未结 7 1668
北荒
北荒 2020-12-16 01:00

I am trying to accomplish the same look of my UISearchBar with a TextField within it, as in my iOS 6 app. I have tried to code it in several ways and not yet been successful

7条回答
  •  [愿得一人]
    2020-12-16 01:07

    Try this out i am not sure but this should work as in iOS 7 searchbar has subview and inside that subview there are two subviews one of which is UITextField

    UIView *searchbarview = [searchBar.subviews objectAtIndex:0];
    UITextField *sbTextField = (UITextField *)[searchbarview.subviews lastObject];
    [sbTextField removeFromSuperview];
    
    CGRect rect = searchBar.frame;
    rect.size.height = 32;
    rect.size.width = 210;
    sbTextField.frame = rect;
    
    [sbTextField setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
    
    
    UIBarButtonItem *searchBarNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:sbTextField];
    
    [[self navigationItem] setLeftBarButtonItem:searchBarNavigationItem];
    

提交回复
热议问题