解决方案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