2013-10-22 22 views
0

我想定製的UISearchBar,但我的「M不好受吧。的UISearchBar定製

enter image description here

這是我的搜索欄看起來像現在,我希望能夠做的是什麼在整個uisearchbar上設置一個uiimage,我嘗試了setSearchFieldBackgroundImage,但是它不會影響到搜索欄的文本字段部分,我怎樣才能把圖像放在白色部分?我也想把白色部分延伸到邊緣以獲得相等的邊距

+0

http://stackoverflow.com/questions/19226922/uitextfield-within-uisearchbar-failing-after-ios-7-upgrade/19227102#19227102 – Rajneesh071

+0

使用的UITextField,並把它在UIVIew()中。 –

回答

0

爲了圖像的目的你可以用這個,不要忘了導入Quar tzcore框架

[searchbar setBackgroundImage:[UIImage imageNamed:@"base_200x50.png"]]; 
    [searchbar setTranslucent:YES]; 
    UITextField *txtSearchField = [searchbar valueForKey:@"_searchField"]; 
    [txtSearchField setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"base_200x50.png"]]]; 
    [txtSearchField setBorderStyle:UITextBorderStyleRoundedRect]; 
    txtSearchField.layer.borderWidth = 8.0f; 
    txtSearchField.layer.cornerRadius = 10.0f; 
    txtSearchField.layer.borderColor = [UIColor clearColor].CGColor; 

並擴展textfeild你可以設置txtSearchField的框架。

0

我希望這可以幫助你:

圖案像我用:

enter image description here

@IBOutlet weak var sbSearchBar: UISearchBar! 

// if you wan to set background color for searchbar, except field 
func setupSearchbarBackgroundImage() -> Void { 

    sbSearchBar.setBackgroundImage(UIImage(named: "pattern2"), for: UIBarPosition.any, barMetrics: UIBarMetrics.default) 

} 

結果:

enter image description here

func setupSearchbar() -> Void { 
    // if you wan to set background color for field only 
    if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField { 

     textfield.backgroundColor = UIColor(patternImage: UIImage(named: "pattern2")!) 

    } 
} 

下面是結果:

enter image description here