2016-02-19 35 views
0

我試圖在開始編輯搜索欄內容時擴展UISearchBar的高度。我正在執行UISearchBarController中的以下代碼。如何使uisearch欄中的文本框顯示在頂部而不是中心?

func searchBarTextDidBeginEditing(searchBar: UISearchBar) { 
    customSearchBar.showsCancelButton = true 
    customDelegate.didStartSearching() 

    var newFrame:CGRect = customSearchBar.frame; 
    newFrame.size = CGSizeMake(customSearchBar.frame.width, customSearchBar.frame.height + 35); 
    customSearchBar.frame = newFrame; 
} 

然而,當我不延長其長度它將很好地工作,之後我伸出長度,UITextField得到自動對準垂直於框架的中心,這是我不希望發生的事情。

enter image description here

如何,我能夠力文本域在原來的位置展示?

+0

什麼是您想要的原始位置?什麼是customSearchBar?和searchBar一樣嗎? –

+0

你在使用約束嗎? –

+0

不...文本框本身是默認的.. –

回答

0

你可能不應該這樣做,但你可以改變:

var newFrame:CGRect = customSearchBar.frame 
    newFrame.size = CGSizeMake(customSearchBar.frame.width, customSearchBar.frame.height + 35) 
    customSearchBar.frame = newFrame 

要:

var newFrame:CGRect = customSearchBar.frame 
    newFrame.size = CGSizeMake(customSearchBar.frame.width, customSearchBar.frame.height + 35); 
    newFrame.origin = CGPoint(x: 10, y: 10) 
    customSearchBar.frame = newFrame 

,看看是否有效。

0

textfield和搜索欄控件是單個控件。如果更改搜索欄的框架,其中的文本字段將自動垂直居中。相反,如果你想要你正在尋找的行爲,那麼你應該把搜索欄放在另一個視圖中作爲子視圖,然後改變視圖的高度。您可以使用自動佈局將searchBar放置在該父視圖中。

+0

請注意,您可以隨時[編輯](http://stackoverflow.com/posts/35507102/edit)您的答案;請避免在同一頁面上多次發帖。謝謝。 – Moritz

相關問題