iOS 11 Navigationitem.Titleview Width Not Set

iOS 11 navigationItem.titleView Width Not Set

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:

Sample Image

iOS 11 Navigation TitleView misplaced

Here's how it can be fixed -

Add this code in the custom title view class -

override var intrinsicContentSize: CGSize {
return UILayoutFittingExpandedSize
}

And the custom title view shows up at the correct position.

Adjusting navigationItem.titleView's frame?

Sometime in between your view controller's viewWillAppear: & viewDidAppear:, the navigation bar is repositioning your titleView and resizing it to fit if necessary. It can end up uncentered because the navigation bar is prefering not to resize your titleView to fit between the buttons. It seems to try to center the title view if it's small enough, but if not will move your titleView off-center, and then I think only as last resort will resize. The effect is that the titleView is taking up all the space between the two buttons, if its textAlignment is centered then it will centered in that space though not the centered with respect to the whole screen. You can see this in your screen shot.

One answer is to make your titleView narrower so the navigation bar can center it, so try about 160 instead of 180 when you set your titleView.frame. It sucks that, when creating the view programmatically like you are, one has to put an estimate for that width into the code. What would be good is a way to maximize that space while staying centered.

It's possible to start with the titleView being the screen width, then after the navigation bar changes titleView.frame, update it again yourself in the view controller's viewDidAppear:. Using its adjusted titleView.frame.origin.x and size.width along with the screen width, you can calculate the largest of the left & right margins, then set the origin.x to that, the size.width to the screen width minus that times 2. However that doesn't take effect until after the pushed view has animated in and the shift afterwards is visible. You could hide the titleView in viewWillAppear: then unhide in viewDidAppear: after centering it, so instead of sliding in with an off-center titleView which is then shifted, it would slide in with no title which then appears.

A better possibility is to make your titleView your own subclass of UIView (or UILabel or whichever) and override setFrame to resize itself to stay centered in its superview. If I ever end up doing this myself I'll post it here. Or maybe someone else knows another place to change the titleView's frame after its been updated but before the view slides in without a view subclass.

iOS 11 - UINavigationItem titleView when using Large Titles mode

I was able to replace the navigation bar big title with a custom view by using a subclass of UINavigationBar and manually changing the view hierarchy :

@interface MYNavigationBar : UINavigationBar

@end
@implementation MYNavigationBar

// ...

- (void)layoutIfNeeded
{
[self setupTitle];
[super layoutIfNeeded];
}

- (void)setupTitle
{
// UINavigationBar
// -> ...
// -> _UINavigationBarLargeTitleView
// -> UILabel << Big Title Label
// -> UIView
// -> UILabel << Big Title Label animating from back button during transitions

for (UIView *view in self.subviews) {
NSString *className = NSStringFromClass(view.classForCoder);
if ([className containsString:@"LargeTitleView"]) {
for (UIView *view2 in view.subviews) {
if ([view2 isKindOfClass:[UILabel class]]) {
[self convertLabel:(UILabel *)view2];
}
for (UIView *view3 in view2.subviews) {
if ([view3 isKindOfClass:[UILabel class]]) {
[self convertLabel:(UILabel *)view3];
}
}
}
}
}
}

- (void)convertLabel:(UILabel*)label
{
// I kept the original label in the hierarchy (with the background color as text color)
// and added my custom view as a subview.
// This allow the transformations applied to the original label to be also applied to mine.
}

Please note that Apple might reject your app because of this trick.

titleView in NavigationItem doesn't consider frame height in iOS 11

we can control the size and position of UINavigationbar titleview. Don't use to set the imageview as titleview directly. in here create a custom UIView and then set the frame as what you need requirement and add the logo as its subview

do like

let supportVie = UIView(frame: CGRect(x: 0, y: 0, width: 70, height: 25))
// Here you can set View width and height as per your requirement for displaying supportVie position in navigationbar
//supportVie.backgroundColor = UIColor.red
let logo = UIImageView(image: UIImage.logo ) //UIImage(named: "SelectAnAlbumTitleLettering")
logo.frame = CGRect(x: 45, y: 5, width: supportVie.frame.size.width, height: supportVie.frame.size.height)
// customize the origin as (45,5) but can pass them as your requirement.
supportVie.addSubview(logo)
//supportVie.contentMode = .center;
navigationItem.titleView = supportVie

NavigationItem TitleView constraint not working

I solved my problem. Instead of using the titleView, I put the textfield in the right barButtonItem and set its width to view.frame.size.width. This gives me a textfield that occupy the whole navigation. I attached the code here incase someone also wants to do it.

searchBar = UISearchBar()
searchBar.showsCancelButton = false
searchBar.placeholder = "Course name"
searchBar.delegate = self
cancelbtn = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(CourseTableViewController.searchCancelled))
self.navigationItem.leftBarButtonItem = cancelbtn

searchTextField = UITextField()
searchTextField.borderStyle = .roundedRect
searchTextField.widthAnchor.constraint(equalToConstant: view.frame.size.width - cancelbtn.width)
searchTextField.translatesAutoresizingMaskIntoConstraints = false
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: searchTextField)
self.navigationItem.rightBarButtonItem?.width = view.frame.size.width

custom titleView of navigationItem is not getting tapped on iOS 11

In iOS 11, titleView is getting set with Autolayout. Hence, the size of the titleView is the intrinsic size of the view you are setting in titleView.

This code in your view class(which you are setting as titleView) should help:

override var intrinsicContentSize: CGSize {
return UILayoutFittingExpandedSize
}

Why is the navigation bar content showing on iOS 11 but not IOS 10?

You need to set label's frame. titleView is subclass of UIView. So It doesn't have intrinsic contentSize. However, iOS 11 provides intrinsic content size for titleView. So you don't need to set its frame. Check this answer.

iOS 11 navigationItem.titleView Width Not Set

func title(text: String) -> UILabel {
let label = UILabel()
// add frame
label.frame = CGRect(x: 0, y: 0, width: 32, height: 32)
label.text = text
label.textColor = UIColor.black
label.font = UIFont.boldSystemFont(ofSize: label.font.pointSize)
return label
}


Related Topics



Leave a reply



Submit