2014-02-11 63 views
1

我試圖做一個UISearchBar矩形,而不是圓形的,但所有的解決方案,我發現迄今(主要是通過迭代子視圖)似乎在iOS 7破矩形的UISearchBar在iOS 7

我做了一些研究自己事實證明,它只有一個UIView子視圖,其中有子視圖,UISearchBarBackgroundUISearchBarTextField(它們都是私有類)。 我試圖

if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { 
    [view removeFromSuperview]; 
} 

if ([view conformsToProtocol:@protocol(UITextInputTraits)]) { 
    @try { 

     [(UITextField *)view setBorderStyle:UITextBorderStyleRoundedRect]; 
    } 
    @catch (NSException * e) { 
     // ignore exception 
    } 
} 

其中view是一個UIView子視圖的子視圖,但沒有人似乎工作。

+0

事實證明,問題部分原因是我將UISearchBar的搜索樣式(在IB中)設置爲最小。如果你將它設置爲Prominent,那麼第一段代碼就可以工作。 (實際上,它仍然是圓形的,所以不是完美的解決方案。如果有人有更好的想法,請分享它。) – Rickye

+0

您可能想要將其作爲答案發布。 – Marco

+0

@Marco看到我對該評論的編輯,這不是最好的解決方案,但是如果沒有人在一段時間內提出更好的解決方案,我會發布它。 – Rickye

回答

1

嘗試......(我知道這還用子視圖,但它工作在ios7)

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, 320, 49)]; 
[self.view addSubview:searchBar]; 
[self checkSubViewsOfView:searchBar WithTabIndex:@""]; 

,並添加這個方法

-(void)checkSubViewsOfView:(UIView *)view WithTabIndex:(NSString *)strTab 
{ 
    if ([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) 
    { 
     view.layer.borderWidth = 1; 
     view.layer.borderColor = [[UIColor whiteColor] CGColor]; 
     return; 
    } 

    for (UIView *vvv in view.subviews) 
    { 
     NSLog(@"%@%@",strTab,[vvv description]); 

     if (vvv.subviews > 0) 
     { 
      NSString *str = [NSString stringWithFormat:@"____%@",strTab]; 
      [self checkSubViewsOfView:vvv WithTabIndex:str]; 
     } 
    } 
} 
+0

好的,謝謝!密切關係我最終與。這個問題是它殺死了UISearchBar的大小調整,因此取消按鈕從不出現在它旁邊。 – Rickye

+0

我的不好,這個工作非常好,謝謝! – Rickye

+0

@Rickye如果你使用這種方法。這是壞的。因爲它不是來自公衆文件。所以用Thorsten說的方法是最好的 – codercat

1

可以設置searchfield背景是這樣的:

[self.searchBar setSearchFieldBackgroundImage:[[UIImage imageNamed:@"searchbar_stretch-0-10-0-10"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)] forState:UIControlStateNormal]; 

和搜索欄背景是這樣的:

[self.searchBar setBackgroundImage:[UIImage imageNamed:@"categories_navbar"]];