2016-02-19 60 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