When you have a UIView as subview inside CustomTitleView, intrinsicContentSize solution does not work, for me in XCODE 9 in iOS 11 only. so I did like below, works fine for me, might this help someone.
return UILayoutFittingExpandedSize not helped me, because view was added vertically few more times to fill layout.
The solution was to override intrinsicContentSize in custom view setting width to max screen width:
- (CGSize)intrinsicContentSize {
//fills empty space. View will be resized to be smaller, but if it is too small - then it stays too small
CGRect frame = self.frame;
frame.size.width = MAX(SCREEN_WIDTH, SCREEN_HEIGHT);
return frame.size;
}
I had to fit an UIImageView as navigationItem.titleView. The aspect ratio did fit but the intrinsicContentSize made it to large.
Scaling the image led to poor image quality.
Setting layout anchors worked for me:
Try using the Standard UISearchBar / UISearchController
Actually what you need to do - if you can use a standard UISearchBar / a UISearchController is to display the search bar the following way which respects the safe area and thus looks perfect on iPhone X and in each device orientation:
If your custom title view is a view that already has an intrinsic content size by default (other than .zero), for example a UILabel, a UITextView or a UIButton, you can simply set
and it will automatically adjust to just enclose its contents, but never overlap with the left and right item views.
For example, you can drag a button into the title view area of a navigation bar in Interface Builder, create an outlet titleButton for it in your view controller and then do
all you have to do is setting translatesAutoresizingMaskIntoConstraints to false
check apple doc
Note that the autoresizing mask constraints fully specify the view’s size and position; therefore, you cannot add additional constraints to modify this size or position without introducing conflicts. If you want to use Auto Layout to dynamically calculate the size and position of your view, you must set this property to false