如何使uisearch栏中的文本字段显示在顶部而不是中心?

时间:2016-02-19 13:40:22

标签: ios swift uisearchbar

当我开始编辑搜索栏内容时,我正在尝试扩展UISearchBar的高度。我正在UISearchBarController中执行以下代码。

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

我如何强制文本字段显示在原始位置?

2 个答案:

答案 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

看看是否有效。

答案 1 :(得分:0)

文本字段和搜索栏控件是单个控件。如果更改搜索栏的框架,则其中的文本字段会自动垂直居中。相反,如果您想要您正在寻找的行为,那么您应该将搜索栏放在另一个视图中作为子视图,然后更改该视图的高度。您可以使用自动布局将searchBar定位在该父视图中。

相关问题