2017-10-05 36 views
5

Tôi có một ứng dụng iOS, trong đó tôi đang thiết lập chế độ xem tiêu đề điều hướng tùy chỉnh.Tiêu đề điều hướng iOS 11Xem không đúng chỗ

Đã hoạt động tốt cho đến iOS 10, nhưng trong iOS 11 chế độ xem tiêu đề điều hướng bị đặt không đúng chỗ.

Dưới đây là ảnh chụp màn hình dành cho iOS 10 -

The title view looks fine

Dưới đây là ảnh chụp màn hình dành cho iOS 11 -

The title view is shifted down

Như bạn có thể thấy trong ảnh chụp màn hình khi tôi chạy mã trên iOS 10, chế độ xem tiêu đề có vẻ ổn. Nhưng cùng một mã trên iOS 11 thay đổi tiêu đề xem xuống bởi một số điểm ảnh và nó bị cắt.

Đây là cách tôi đang thiết lập các quan điểm tiêu đề -

navigationItem.titleView = MY_CUSTOM_TITLE_VIEW

tôi đã cố gắng rất nhiều thứ và tìm kiếm nhiều giải pháp nhưng không được làm việc.

+0

Bản sao có thể có của [iOS 11 navigationItem.titleView Width Not Set] (https://stackoverflow.com/questions/44932084/ios-11-navigationitem-titleview-width-not-set) – Dania

Trả lời

13

Sau đây là cách nó có thể được cố định -

Thêm mã này trong lớp tùy chỉnh tiêu đề xem -

override var intrinsicContentSize: CGSize { 
    return UILayoutFittingExpandedSize 
} 

Và chế độ xem tiêu đề tùy chỉnh hiển thị ở vị trí chính xác.

+0

Nó hoạt động cho tôi. Nhưng nếu tôi chỉ có mục nút trái/phải, chế độ xem tiêu đề sẽ không hiển thị ở chiều ngang trung tâm tuyệt đối. – Lumialxk

1

Có vấn đề với thanh điều hướng mới cho iOS, khi bạn thêm chế độ xem tùy chỉnh vào chế độ xem tiêu đề. Vì vậy, bạn chỉ cần thêm "prefertsLargeTitles" là No & "largeTitleDisplayMode" là DisplayModeNever trước khi thực hiện tùy chỉnh thanh tùy chỉnh.

Ở đây mã của tôi:

if (@available(iOS 11.0, *)) { 
    [[self navigationController] navigationBar].prefersLargeTitles = NO; 
    [[self navigationController] navigationItem].largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever; 
    } 
    // Add contraints to titleView 
    NSLayoutConstraint *centerPrompt= [NSLayoutConstraint constraintWithItem:midPromptLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:midView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; 
    NSLayoutConstraint *topPrompt= [NSLayoutConstraint constraintWithItem:midPromptLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:midView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]; 
    NSLayoutConstraint *centerTitle= [NSLayoutConstraint constraintWithItem:midTitleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:midView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; 
    NSLayoutConstraint *topTitle= [NSLayoutConstraint constraintWithItem:midTitleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:midPromptLabel attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]; 

    [midView addConstraints:@[centerPrompt,topPrompt,centerTitle,topTitle]]; 

Hope sẽ giúp bạn^_^

+0

Cảm ơn câu trả lời của bạn nhưng nó không làm việc cho tôi. Tuy nhiên tôi đã thử một cái gì đó và nó đã làm việc. –

+0

@PrateekVarshney Có thể chia sẻ những gì bạn đã thử làm việc ?! –

+0

Có Tôi đã thêm nó làm câu trả lời –

Các vấn đề liên quan