5

Tôi muốn có một UIView tùy chỉnh trong UINavigationBar của tôi giữa một trái và một BarButtonItem bên phải nhưng càng rộng càng tốt. Vì lý do này tôi đã thêm một UIView trong IB vào NavigationBar. Với autolayout vô hiệu hóa tất cả mọi thứ hoạt động như mong đợi với các mặt nạ autoresizing. Nhưng trong bảng phân cảnh của tôi với tính năng tự động bật, tôi không thể làm cho nó hoạt động. Có vẻ như tôi không thể đặt bất kỳ ràng buộc nào trong IB cho titleView. Nếu tôi xoay thiết bị của mình sang chế độ ngang, UIView vẫn có cùng chiều rộng. Tôi phải làm gì để tiêu đềView lấp đầy khoảng trống giữa UIBarButtonItems với tính năng tự động điền được bật?Tự động xem tiêu đề trong một NavigationBar với autolayout

Cảm ơn bạn đã giúp đỡ bất kỳ

Linard

Trả lời

4

tôi giải quyết vấn đề này như sau trong mã của tôi:

- (void)willAnimateRotationToInterfaceOrientation:(__unused UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; 

    CGSize navigationBarSize = self.navigationController.navigationBar.frame.size; 
    UIView *titleView = self.navigationItem.titleView; 
    CGRect titleViewFrame = titleView.frame; 
    titleViewFrame.size = navigationBarSize; 
    self.navigationItem.titleView.frame = titleViewFrame; 
} 

tôi đã không tìm thấy một giải pháp khác (với thay đổi kích thước tự động), nhưng tôi mở cửa cho các giải pháp mới và/hoặc tốt hơn

Linard

-4

Bạn có thể thêm các ràng buộc trong mã.

Trong viewDidLoad, bạn có thể làm một cái gì đó như thế này:

UIView *titleView = [[UIView alloc] initWithFrame:CGRectZero]; 
self.navigationItem.titleView = titleView; 
UIView *titleViewSuperview = titleView.superview; 
titleView.translatesAutoresizingMaskIntoConstraints = NO; 
[titleViewSuperview addConstraint:[NSLayoutConstraint constraintWithItem:titleView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:titleViewSuperview attribute:NSLayoutAttributeLeading multiplier:1 constant:0]]; // leading 
[titleViewSuperview addConstraint:[NSLayoutConstraint constraintWithItem:titleView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:titleViewSuperview attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; // top 
[titleViewSuperview addConstraint:[NSLayoutConstraint constraintWithItem:titleViewSuperview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:titleView attribute:NSLayoutAttributeWidth multiplier:1 constant:0]]; // width 
[titleViewSuperview addConstraint:[NSLayoutConstraint constraintWithItem:titleViewSuperview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:titleView attribute:NSLayoutAttributeHeight multiplier:1 constant:0]]; // height 
+0

Cảm ơn bạn đã gợi ý của bạn. Tôi sẽ thử nó ngay sau khi tôi có thời gian ... –

+0

Tôi nhận được một sự cố về hạn chế chiều rộng bởi vì titleSuperView là null. Có ai khác có may mắn không? – djibouti33

+6

Ít nhất trong iOS 7, bạn không thể sửa đổi các ràng buộc cho UINavigationBar được quản lý bởi một bộ điều khiển – Andy