约束不适用于 UINavigationBar 项



我试图给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;
}
}
}

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

也许你失去了这个: self.view.clipsToBounds = true

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

最新更新