Tôi đang cố gắng có hai UILabels trong thanh điều hướng thay vì chỉ một.ios Tiêu đề và phụ đề trong Thanh Điều hướng ở giữa
Tôi đi theo liên kết này để có thông tin về làm thế nào để làm điều đó: iPhone Title and Subtitle in Navigation Bar
Nó hoạt động tốt, nhưng tôi có thể không nhận được văn bản của tôi để được tập trung đúng. Nó nằm giữa các nút, nhưng hành vi tiêu đề mặc định là căn giữa chính nó ngay dưới thời gian.
tôi đã có một cái nhìn ở đây, cùng một câu hỏi, nhưng không có câu trả lời: UINavigationBar TitleView with subtitle
tôi thiếu gì? Đây là mã của tôi:
CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44);
UIView* _headerTitleSubtitleView = [[UILabel alloc] initWithFrame:headerTitleSubtitleFrame];
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor];
_headerTitleSubtitleView.autoresizesSubviews = NO;
CGRect titleFrame = CGRectMake(0, 2, 200, 24);
UILabel *titleView = [[UILabel alloc] initWithFrame:titleFrame];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20];
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
titleView.shadowColor = [UIColor darkGrayColor];
titleView.shadowOffset = CGSizeMake(0, -1);
titleView.text = @"Title";
titleView.adjustsFontSizeToFitWidth = YES;
[_headerTitleSubtitleView addSubview:titleView];
CGRect subtitleFrame = CGRectMake(0, 24, 200, 44-24);
UILabel *subtitleView = [[UILabel alloc] initWithFrame:subtitleFrame];
subtitleView.backgroundColor = [UIColor clearColor];
subtitleView.font = [UIFont boldSystemFontOfSize:13];
subtitleView.textAlignment = NSTextAlignmentCenter;
subtitleView.textColor = [UIColor whiteColor];
subtitleView.shadowColor = [UIColor darkGrayColor];
subtitleView.shadowOffset = CGSizeMake(0, -1);
subtitleView.text = @"Subtitle";
subtitleView.adjustsFontSizeToFitWidth = YES;
[_headerTitleSubtitleView addSubview:subtitleView];
self.navigationItem.titleView = _headerTitleSubtitleView;
Cảm ơn rất nhiều! Nó hoạt dộng bây giờ. Nhưng tôi không hiểu. Tại sao nó không tập trung khi kích thước cho trước cao hơn? – user2700551
Nó không hoạt động cho tất cả các trường hợp khác nhau. Mặc dù nó hoạt động cho nút quay lại này, nút quay lại của tôi ở bên trái có thể rộng hơn và tiêu đề được dịch sang phải. – user2700551
Luôn luôn văn bản sẽ được căn giữa vào nhãn. vì vậy bạn nên điều chỉnh chiều rộng nhãn cho phù hợp. nếu đặt backgroundcolor cho nhãn để kiểm tra, thì bạn sẽ nhận được ý tưởng rõ ràng. Ngoài ra nó sẽ được đánh giá cao, nếu bạn có thể chấp nhận và upvote câu trả lời vì nó đã giúp bạn. –