IOS 11 - UINavigationItem titleView when using large title mode

I am trying to understand either the error or the expected behavior.

In iOS 10 and earlier, we could create a custom header using navigationItem.titleView.
On iOS 11, when you configure navigationItem.largeTitleDisplayMode = .alwaysand configure navigationItem.titleView = <Some cool UIButton>, both the normal navigation title bar and the large navigation title are displayed .

Illustration: enter image description here

Summarizing:

How can we use a custom header in our large navigation header?

EDIT: Expected Result:

enter image description here

+7
source share
2 answers

, UINavigationBar :

@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.
}

, Apple - .

+1

, . , iOS 11 .

, , , . (iOS 11) (HIG) . HIG . , .


, Single View. .

ViewController.swift viewDidLoad :

    let titleButton = UIButton(type: .roundedRect)
    titleButton.setTitle("Hello Button!", for: UIControlState.normal)

    let navController = parent as! UINavigationController

    navController.navigationBar.topItem!.title = "Hello???"
    navController.navigationBar.topItem!.titleView = titleButton
    navController.navigationBar.prefersLargeTitles = true

.

:

  • .title : navbar , ( , , )

  • .prefersLargeTitles false: - , , .

  • titleView :

    • .prefersLargeTitles true: title , .
    • .prefersLargeTitles false: title , .

: GitHub

+6

Source: https://habr.com/ru/post/1686446/


All Articles