如何修复导航栏项目在按下 VC 后动画到一边?
How to fix Navigation Bar Item animating to side after pushing a VC?
我有一个 iOS 应用程序,我在其中为导航项创建自定义视图。
导航项效果很好,每次我来到屏幕时都会自动定位。
然而,当 VC 被从该屏幕推送并且我返回到该屏幕时,导航项暂时出现在正确的位置,但立即转移到右侧角落。
我确实怀疑这与过渡期间导航栏的动画有关,但即使在尝试了多种解决方法之后,它仍然会发生。
下面是导航项视图的代码。
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
v.backgroundColor = [UIColor clearColor];
v.opaque = NO;
UILabel *subtitleLabel = [[UILabel alloc] initWithFrame:CGRectIntegral(CGRectMake(v.bounds.origin.x, v.bounds.origin.y + v.bounds.size.height * 0.6, v.bounds.size.width, v.bounds.size.height * 0.3))];
subtitleLabel.text = subtitle;
subtitleLabel.font = [UIFont systemFontOfSize:11];//Font adjusted to accomodate text into size
subtitleLabel.backgroundColor = [UIColor clearColor];
subtitleLabel.opaque = NO;
subtitleLabel.textAlignment = NSTextAlignmentCenter;
subtitleLabel.textColor = defaultSubtitleTextColor;
subtitleLabel.shadowOffset = defaultShadowOffset;
subtitleLabel.shadowColor = defaultShadowColor;
[v addSubview:subtitleLabel];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectIntegral(CGRectMake(v.bounds.origin.x, v.bounds.origin.y + v.bounds.size.height * 0.1, v.bounds.size.width, v.bounds.size.height * 0.5))];
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:19];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.opaque = NO;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = defaultTextColor;
titleLabel.shadowOffset = defaultShadowOffset;
titleLabel.shadowColor = defaultShadowColor;
[v addSubview:titleLabel];
navItem.titleView = v;
已经在这上面花了很多时间,我们将不胜感激任何帮助。
对于处于类似情况的任何其他人,这与 intrinsicContentSize
有关。设置为returnUILayoutFittingExpandedSize
后。问题消失了。
我有一个 iOS 应用程序,我在其中为导航项创建自定义视图。 导航项效果很好,每次我来到屏幕时都会自动定位。
我确实怀疑这与过渡期间导航栏的动画有关,但即使在尝试了多种解决方法之后,它仍然会发生。 下面是导航项视图的代码。
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
v.backgroundColor = [UIColor clearColor];
v.opaque = NO;
UILabel *subtitleLabel = [[UILabel alloc] initWithFrame:CGRectIntegral(CGRectMake(v.bounds.origin.x, v.bounds.origin.y + v.bounds.size.height * 0.6, v.bounds.size.width, v.bounds.size.height * 0.3))];
subtitleLabel.text = subtitle;
subtitleLabel.font = [UIFont systemFontOfSize:11];//Font adjusted to accomodate text into size
subtitleLabel.backgroundColor = [UIColor clearColor];
subtitleLabel.opaque = NO;
subtitleLabel.textAlignment = NSTextAlignmentCenter;
subtitleLabel.textColor = defaultSubtitleTextColor;
subtitleLabel.shadowOffset = defaultShadowOffset;
subtitleLabel.shadowColor = defaultShadowColor;
[v addSubview:subtitleLabel];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectIntegral(CGRectMake(v.bounds.origin.x, v.bounds.origin.y + v.bounds.size.height * 0.1, v.bounds.size.width, v.bounds.size.height * 0.5))];
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:19];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.opaque = NO;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = defaultTextColor;
titleLabel.shadowOffset = defaultShadowOffset;
titleLabel.shadowColor = defaultShadowColor;
[v addSubview:titleLabel];
navItem.titleView = v;
已经在这上面花了很多时间,我们将不胜感激任何帮助。
对于处于类似情况的任何其他人,这与 intrinsicContentSize
有关。设置为returnUILayoutFittingExpandedSize
后。问题消失了。