Seeing a behavior on iOS11 with a navigationItem.titleView where the width of the titleView is not the full width of the screen.
I have a custom view that I set as t
setting intrinsicContentSize
to UILayoutFittingExpandedSize
works fine as well
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.
@interface CustomTitleView : UIView
@property (weak, nonatomic) IBOutlet UIView *doubleTitleView;
@end
@implementation CustomTitleView
- (void)awakeFromNib {
[super awakeFromNib];
int width = _doubleTitleView.frame.size.width;
int height = _doubleTitleView.frame.size.height;
if (width != 0 && height != 0) {
NSLayoutConstraint *widthConstraint = [_doubleTitleView.widthAnchor constraintEqualToConstant:width];
NSLayoutConstraint *heightConstraint = [_doubleTitleView.heightAnchor constraintEqualToConstant:height];
[_doubleTitleView addConstraint:heightConstraint];
[_doubleTitleView addConstraint:widthConstraint];
[heightConstraint setActive:TRUE];
[widthConstraint setActive:TRUE];
}
}
Fixed it by creating a subclass of UIView and assigned it to a title view of UINavigationController
Objective-C:
#import "FLWCustomTitleView.h"
@implementation FLWCustomTitleView
- (CGSize )intrinsicContentSize {
return UILayoutFittingExpandedSize;
}
@end
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 figured it out. I had to override the intrinsicContentSize getter for the view, and the text field.
I set the width to CGFloat.greatestFiniteMagnitude so it'll always be as wide as the screen.
Update:
Since I've spent couple of hours on this issue, hope that some else will catch up faster by having all things tight up together
I've created a custom sub class of TitleView
, called CustomTitleView
, here's the code:
import UIKit
class CustomTitleView: UIView {
override var intrinsicContentSize: CGSize {
return UIView.layoutFittingExpandedSize
}
}
and the most important part which I missed from the start was this:
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
yourCustomTitleView.translatesAutoresizingMaskIntoConstraints = false
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
override func viewDidLoad() {
super.viewDidLoad()
titleButton.translatesAutoresizingMaskIntoConstraints = false
}