解決方案1:每次你要刪除自定義的UIView對象時添加到導航欄
[self.navigationController.navigationBar.subviews enumerateObjectsWithOptions:0 usingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isMemberOfClass:[UILabel class]]) {
[obj removeFromSuperview];
}
}];
UILabel *lblTitle = [UILabel new];
lblTitle.text = text;
CGSize lblSize = [Utility sizeOfText:text withFont:kCGFontMedium(19)];
lblTitle.frame = CGRectMake(60, 9, lblSize.width, lblSize.height);
lblTitle.font = kCGFontMedium(19);
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
[lblTitle sizeToFit];
[self.navigationController.navigationBar addSubview:lblTitle];
解決方案2:在類接口中添加屬性以存儲對自定義子視圖的訪問EW中的導航欄
@property (weak, readwrite, nonatomic) UILabel *navSubView;
[self.lblTitle removeFromSuperview];
self.lblTitle = [UILabel new];
lblTitle.text = text;
CGSize lblSize = [Utility sizeOfText:text withFont:kCGFontMedium(19)];
lblTitle.frame = CGRectMake(60, 9, lblSize.width, lblSize.height);
lblTitle.font = kCGFontMedium(19);
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
[lblTitle sizeToFit];
[self.navigationController.navigationBar addSubview:lblTitle];
的問題是每次添加新的子視圖,而不是替換原有的子視圖。嘗試將舊子視圖設置爲零,或者使用新子視圖重新分配舊子視圖。 – RPK
@RPK如何更換舊的Subivew..Means如何在新的屏幕中獲取Old Subview的引用 – Dalvik
當您在UINavigationController中推送並彈出視圖控制器時,視圖控制器可能會更改,但導航控制器保持不變。您可以嘗試對UINavigationController進行子分類並保留對導航欄子視圖的類引用。然後,您可以在viewDidLoad中添加子視圖一次,然後在需要時更改它。 – RPK