导航栏的标题视图重叠
我在导航栏中添加了自定义的标题视图。导航栏的标题视图重叠
在每次屏幕更改时,我都会添加另一个标题视图。
问题是前一个没有被删除,我可以一次查看所有视图。
这里是我的代码:
UILabel *lblTitle = [[UILabel alloc] init]; 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];
我的问题: 注册并登录,重叠
回答:
解决方案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];
回答:
或者你可以只使用NavigationItem的 “titleview的” 属性
的UILabel * titleLabel = [[的UILabel的alloc]初始化]。你可以使用下面的代码来实现这个功能:* titleView = [[UIView alloc] initWithFrame:titleLabel.frame]; [titleView addSubview:titleLabel];
self.navigationItem.titleView = titleView;
这将确保只有一个标题标签的实例存在,它不会重叠self.title要么
以上是 导航栏的标题视图重叠 的全部内容, 来源链接: utcz.com/qa/261390.html