gpt4 book ai didi

ios - 使用程序化自动布局时容器 View 的框架设置为 CGRectZero

转载 作者:可可西里 更新时间:2023-11-01 03:29:56 26 4
gpt4 key购买 nike

我有一个 UIView 子类,它将是一个工具提示,可以出现在任何 View Controller 的底部,以显示提示/错误消息/成功消息/等等。工具提示 View 有两个 subview :左侧的 UILabel 和右侧的 UIView,可以是任何东西(但通常用作 UIButton).

TooltipView.h

@interface TooltipView : UIView

@property (nonatomic, readonly) UILabel *textLabel;

@property (nonatomic) UIView *rightView;

TooltipView.m

static CGFloat const kSubviewToSuperviewSpacing = 7.0f;
static CGFloat const kTextLabelToRightViewHorizontalSpacing = 7.0f;

@implementation TooltipView

- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];

if (self) {
_textLabel = [[UILabel alloc] init];
_textLabel.numberOfLines = 0;
[_textLabel setTranslatesAutoresizingMaskIntoConstraints:NO];

[self addSubview:_textLabel];
}

return self;
}

- (void)setRightView:(UIView *)rightView {
if (_rightView != rightView) {
_rightView = rightView;

[self addSubview:_rightView];

[self setNeedsDisplay];
}
}

- (BOOL)translatesAutoresizingMaskIntoConstraints {
return NO;
}

- (void)updateConstraints {
self.didUpdateConstraints = YES;

[self addConstraint:[NSLayoutConstraint constraintWithItem:self.textLabel
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:kSubviewToSuperviewSpacing]];

NSMutableDictionary *metricsDictionary = [@{@"subviewToSuperviewSpacing": @(kSubviewToSuperviewSpacing)} mutableCopy];

NSMutableDictionary *viewsDictionary = [@{@"textLabel": self.textLabel} mutableCopy];

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-subviewToSuperviewSpacing-[textLabel]-subviewToSuperviewSpacing-|"
options:0
metrics:metricsDictionary
views:viewsDictionary]];

if (self.rightView) {
metricsDictionary[@"textLabelToRightViewHorizontalSpacing"] = @(kTextLabelToRightViewHorizontalSpacing);
viewsDictionary[@"rightView"] = self.rightView;

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-subviewToSuperviewSpacing-[rightView]-subviewToSuperviewSpacing-|"
options:0
metrics:metricsDictionary
views:viewsDictionary]];

[self addConstraint:[NSLayoutConstraint constraintWithItem:self.rightView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:kSubviewToSuperviewSpacing]];
}

[super updateConstraints];
}

我还有一个 View Controller 来使用 UILabel 作为 rightView 来测试它。它是 UIViewController 的子类,它唯一的方法是重写 loadView:

- (void)loadView {
TooltipView *tooltipView = [[TooltipView alloc] initWithFrame:CGRectZero];

tooltipView.textLabel.text = @"Test 1";

UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectZero];
testLabel.text = @"Hello there";
[testLabel setTranslatesAutoresizingMaskIntoConstraints:NO];

tooltipView.rightView = testLabel;

self.view = tooltipView;

最后,在我想要显示工具提示的 View Controller 中,我将 TooltipViewController 添加为 subview Controller 。最终,这将由某些事件触发,但出于测试目的,我将其添加到 viewWillAppear: 中。

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

self.aViewController = [[TooltipViewController alloc] init];
self.aViewController.view.backgroundColor = [UIColor whiteColor];

[self addChildViewController:self.aViewController];

[self.view addSubview:self.aViewController.view];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|"
options:0
metrics:nil
views:@{@"view": self.aViewController.view}]];

[self.aViewController.view addConstraint:[NSLayoutConstraint constraintWithItem:self.aViewController.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0f
constant:150.0f]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.aViewController.view
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.0f]];

[self.aViewController didMoveToParentViewController:self];
}

但是,在运行时,textLabelrightView 的框架似乎设置正确,但是 tooltipView 的框架设置了到 CGRectZero,这三个 View 都没有出现在屏幕上。

提前致谢!

编辑

我什至尝试过完全删除 textLabelrightView,所以只是一个带有前导、尾随、底部和高度约束的普通 UIView。没有什么是模棱两可的,但 View 的框架仍然是 CGRectZero

最佳答案

事实证明,解决方案只是在 TooltipView 的初始化程序中调用 setTranslatesAutoresizingMaskIntoConstraints 而不是覆盖该方法。这导致 View 正确显示。 h/t 亚伦汤普森 (https://twitter.com/aaptho/status/612815498661773312)。

关于ios - 使用程序化自动布局时容器 View 的框架设置为 CGRectZero,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30928782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com