我刚刚在视图中添加了带有顶部UISearchBar
的UITableView
,但仅在iOS 11上,表视图的宽度比我的搜索栏更多,这是我的代码:
self.autocompleteTableView = UITableView(frame: CGRect(x: self.storeSearchBar.frame.origin.x, y: self.storeSearchBar.frame.origin.y + self.storeSearchBar.frame.size.height, width: self.storeSearchBar.bounds.width, height: 200.0))
self.autocompleteTableView!.alpha = 0.8
self.autocompleteTableView!.delegate = self
self.autocompleteTableView!.dataSource = self
self.autocompleteTableView!.separatorStyle = .none
if #available(iOS 11.0, *) {
self.autocompleteTableView!.insetsContentViewsToSafeArea = true
}
self.storeSearchBar.alpha = 0.8
self.storeSearchBar.delegate = self
self.view.addSubview(self.autocompleteTableView!)
self.autocompleteTableView!.isHidden = true
let searchBar = self.storeSearchBar!
searchBar.barTintColor = UIColor.white
searchBar.sizeToFit()
我在做什么错?
所以,感谢@maulikbhuptani,答案是:
override func viewDidLayoutSubviews() {
self.autocompleteTableView = UITableView(frame: CGRect(x:
self.storeSearchBar.frame.origin.x, y: self.storeSearchBar.frame.origin.y + self.storeSearchBar.frame.size.height, width: self.storeSearchBar.bounds.width, height: 200.0))
self.autocompleteTableView!.alpha = 0.8
self.autocompleteTableView!.delegate = self
self.autocompleteTableView!.dataSource = self
self.autocompleteTableView!.separatorStyle = .none
if #available(iOS 11.0, *) {
self.autocompleteTableView!.insetsContentViewsToSafeArea = true
}
self.storeSearchBar.alpha = 0.8
self.storeSearchBar.delegate = self
self.view.addSubview(self.autocompleteTableView!)
self.autocompleteTableView!.isHidden = true
let searchBar = self.storeSearchBar!
searchBar.barTintColor = UIColor.white
searchBar.sizeToFit()
}