简体   繁体   中英

UISearchBar in iOS 8 - UI displacement

I have UINavigationItem and I want to add UISearchBar to it. Here's how I'm doing that:

let searchBar = UISearchBar(frame: CGRectMake(0, 0, self.navigationController!.navigationBar.bounds.width, self.navigationController!.navigationBar.bounds.height))
//searchBar.sizeToFit()
searchBar.showsCancelButton = true
let barWrapper = UIView(frame: searchBar.frame)
barWrapper.addSubview(searchBar)
self.navigationItem.titleView = barWrapper

Here's result: 在此处输入图片说明

What's wrong here?

sizeToFit() will make the UIScrollView just big enough to fit its subviews.

You need to size it to fit the navigation bar to get rid of the gaps.

EDIT

If you're not using the nav bar for navigation, try just putting the search bar directly in your view. You'll have full control over the frame that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM