use*_*452 7 uiimageview uinavigationcontroller viewwillappear ios uistackview
我在我的应用程序中导航栏的标题视图上设置了 UIStackView。它只是在标题左侧显示应用程序的图标。在 iOS 15 及更早版本中,这显示得很好。然而,现在,当您第一次启动应用程序时,它显示得很好,但是导航到不同的视图时,图标位于该视图的错误位置,并且当返回到父视图时也会位于错误的位置。其图片位于代码下方。
- (void)viewWillAppear:(BOOL)animated
UIImageView *theimageView = [[UIImageView alloc] init];
theimageView.contentMode = UIViewContentModeScaleAspectFit;
theimageView.image = [UIImage imageNamed:nameOfColor];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.font = [UIFont fontWithName:@"Roboto-Bold" size:16];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = self.title;
UIStackView *hStack = [[UIStackView alloc] init];
[hStack addArrangedSubview:theimageView];
[hStack addArrangedSubview:titleLabel];
hStack.axis = UILayoutConstraintAxisHorizontal;
self.navigationItem.titleView = hStack;
[super viewWillAppear:animated];
Run Code Online (Sandbox Code Playgroud)
我能够重现您的问题。我可以确定的一件事是,苹果对 UIKit 的这一部分进行了一些更改。

提供自定义UIStackView作为titleView导航栏是完全可以的。但要正确设置 a,UIStackView您应该添加一些约束。
您应该添加几个约束来帮助了解UIStackView您想要如何布局排列的子视图。
UIImageView *theimageView = [[UIImageView alloc] init];
theimageView.contentMode = UIViewContentModeScaleAspectFit;
theimageView.image = [UIImage systemImageNamed: @"peacesign"];
// Contraints to image height
[theimageView.heightAnchor constraintEqualToConstant: 20].active = true;
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.font = [UIFont fontWithName:@"Roboto-Bold" size:16];
titleLabel.textColor = [UIColor blackColor];
titleLabel.text = self.title;
// Contraints to label height
[titleLabel.heightAnchor constraintEqualToConstant: 20].active = true;
UIStackView *hStack = [[UIStackView alloc] init];
[hStack addArrangedSubview:theimageView];
[hStack addArrangedSubview:titleLabel];
hStack.axis = UILayoutConstraintAxisHorizontal;
// Center the items
hStack.alignment = UIStackViewAlignmentCenter;
// Spacing of 6
hStack.spacing = 6;
self.navigationItem.titleView = hStack;
[super viewWillAppear:animated];
Run Code Online (Sandbox Code Playgroud)
如果添加两个缺失的约束,现在titleView就像 iOS 15 及更早版本一样。
| 归档时间: |
|
| 查看次数: |
751 次 |
| 最近记录: |