约束不适用于UINavigationBar项目

时间:2018-10-19 10:08:09

标签: ios swift uinavigationcontroller constraints

我正在尝试给UISearchbar前导和尾随空间。我几乎尝试了一切。高度,宽度,前后约束,但没有任何效果。请在我以编程方式加载导航栏项时让我知道如何应用约束。

func setupsearchbar() {
    //  Setup the Search Controller
    searchcontroller.searchResultsUpdater = self as? UISearchResultsUpdating
    searchcontroller.hidesNavigationBarDuringPresentation = false
    searchcontroller.searchBar.placeholder = "Search"
    self.navigationItem.titleView = searchcontroller.searchBar
    searchcontroller.searchBar.delegate = self
    searchcontroller.searchBar.translatesAutoresizingMaskIntoConstraints = false
    searchcontroller.searchBar.widthAnchor.constraint(lessThanOrEqualToConstant: 100.0).isActive = true


    if let textfield = searchcontroller.searchBar.value(forKey: "searchField") as? UITextField {

        textfield.textColor = UIColor.blue

        if let backgroundview = textfield.subviews.first {

            // Background color
            backgroundview.backgroundColor = UIColor.white

            backgroundview.layer.cornerRadius = 18
            backgroundview.clipsToBounds = true;
        }
    }

}

请帮助我为什么我的导航栏没有响应约束。

1 个答案:

答案 0 :(得分:0)

也许您会失去这一点:self.view.clipsToBounds = true

backgroundview.layer.cornerRadius = 18
backgroundview.clipsToBounds = true;
self.view.clipsToBounds = true