gpt4 book ai didi

ios - 以编程方式构建带有约束的 titleView(或通常构建带有约束的 View )

转载 作者:IT王子 更新时间:2023-10-29 07:48:04 27 4
gpt4 key购买 nike

我正在尝试构建一个带有如下约束的 titleView:

titleView

我知道我将如何使用框架来做到这一点。我会计算文本的宽度、图像的宽度,创建一个包含该宽度/高度的 View 以包含两者,然后将它们作为 subview 添加到带有框架的适当位置。

我试图了解如何在约束条件下做到这一点。我的想法是内在内容大小会帮助我解决这个问题,但我正在疯狂地尝试让它发挥作用。

UILabel *categoryNameLabel = [[UILabel alloc] init];
categoryNameLabel.text = categoryName; // a variable from elsewhere that has a category like "Popular"
categoryNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
[categoryNameLabel sizeToFit]; // hoping to set it to the instrinsic size of the text?

UIView *titleView = [[UIView alloc] init]; // no frame here right?
[titleView addSubview:categoryNameLabel];
NSArray *constraints;
if (categoryImage) {
UIImageView *categoryImageView = [[UIImageView alloc] initWithImage:categoryImage];
[titleView addSubview:categoryImageView];
categoryImageView.translatesAutoresizingMaskIntoConstraints = NO;
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[categoryImageView]-[categoryNameLabel]|" options:NSLayoutFormatAlignAllTop metrics:nil views:NSDictionaryOfVariableBindings(categoryImageView, categoryNameLabel)];
} else {
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[categoryNameLabel]|" options:NSLayoutFormatAlignAllTop metrics:nil views:NSDictionaryOfVariableBindings(categoryNameLabel)];
}
[titleView addConstraints:constraints];


// here I set the titleView to the navigationItem.titleView

我不应该硬编码 titleView 的大小。它应该能够通过其内容的大小来确定,但是......

  1. 除非我对框架进行硬编码,否则 titleView 确定其大小为 0。
  2. 如果我设置 translatesAutoresizingMaskIntoConstraints = NO 应用程序崩溃并出现此错误:'执行 -layoutSubviews 后仍需要自动布局。 UINavigationBar 实现-layoutSubviews 需要调用super.'

更新

我可以使用这段代码,但我仍然需要在 titleView 上设置框架:

UILabel *categoryNameLabel = [[UILabel alloc] init];
categoryNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
categoryNameLabel.text = categoryName;
categoryNameLabel.opaque = NO;
categoryNameLabel.backgroundColor = [UIColor clearColor];

UIView *titleView = [[UIView alloc] init];
[titleView addSubview:categoryNameLabel];
NSArray *constraints;
if (categoryImage) {
UIImageView *categoryImageView = [[UIImageView alloc] initWithImage:categoryImage];
[titleView addSubview:categoryImageView];
categoryImageView.translatesAutoresizingMaskIntoConstraints = NO;
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[categoryImageView]-7-[categoryNameLabel]|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(categoryImageView, categoryNameLabel)];
[titleView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[categoryImageView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(categoryImageView)];
[titleView addConstraints:constraints];

titleView.frame = CGRectMake(0, 0, categoryImageView.frame.size.width + 7 + categoryNameLabel.intrinsicContentSize.width, categoryImageView.frame.size.height);
} else {
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[categoryNameLabel]|" options:NSLayoutFormatAlignAllTop metrics:nil views:NSDictionaryOfVariableBindings(categoryNameLabel)];
[titleView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[categoryNameLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(categoryNameLabel)];
[titleView addConstraints:constraints];
titleView.frame = CGRectMake(0, 0, categoryNameLabel.intrinsicContentSize.width, categoryNameLabel.intrinsicContentSize.height);
}
return titleView;

最佳答案

我真的需要约束,所以今天就试了一下。我发现有效的是:

    let v  = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
// add your views and set up all the constraints

// This is the magic sauce!
v.layoutIfNeeded()
v.sizeToFit()

// Now the frame is set (you can print it out)
v.translatesAutoresizingMaskIntoConstraints = true // make nav bar happy
navigationItem.titleView = v

像魅力一样工作!

关于ios - 以编程方式构建带有约束的 titleView(或通常构建带有约束的 View ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15285698/

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