gpt4 book ai didi

ios - UILabel 调整其 SuperView 的大小?

转载 作者:行者123 更新时间:2023-11-30 14:19:22 25 4
gpt4 key购买 nike

我有以下 View ,其中包含 UILabel:

class MyView : UIView {

func viewDidLoad() {

self.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth

bottomView = UIView(frame: CGRectMake(self.bounds.origin.x, self.bounds.origin.y + self.imageView!.bounds.size.width, self.bounds.size.width, self.bounds.size.height - self.imageView!.bounds.size.height))

// bottomView frame calculation is: (0.0, 355.0, 355.0, 130.0)

bottomView?.backgroundColor = UIColor.greenColor()
bottomView?.autoresizingMask = UIViewAutoresizing.FlexibleWidth
bottomView?.clipsToBounds = true
self.addSubview(self.bottomView!)

var descriptionRect: CGRect = CGRectInset(self.bottomView!.bounds, leftRightInset, 20/2)
let descriptionLabel = UILabel()
descriptionLabel.numberOfLines = 3
descriptionLabel.autoresizingMask = UIViewAutoresizing.FlexibleWidth
descriptionLabel.font = UIFont(name: MGFont.helvetica, size: 22)
descriptionLabel.textColor = UIColor.whiteColor()
descriptionLabel.textAlignment = NSTextAlignment.Left
descriptionLabel.backgroundColor = UIColor.blueColor()
var paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 1.0
paragraphStyle.lineBreakMode = NSLineBreakMode.ByTruncatingTail
let attributes = [NSParagraphStyleAttributeName : paragraphStyle]
descriptionLabel.attributedText = NSAttributedString(string: previewCard.title, attributes:attributes)
bottomView?.addSubview(descriptionLabel)

descriptionLabel.bounds = descriptionRect
descriptionLabel.sizeToFit()
descriptionLabel.center = CGPointMake(bottomView!.bounds.width/2, bottomView!.bounds.height/2 - hotelNameLableHeight/2)

}

}

bottomView 的高度应始终固定。

MyView 在运行时调整大小。这意味着绿色底部 View 的尺寸也会增加。

这是标签有两行和三行时的结果:

enter image description here

UILabel 似乎调整了其 super View 的大小。

请注意,我不使用自动布局。

  override func layoutSubviews() {
super.layoutSubviews()
}

如何防止 UILabel 调整其 SuperView 的大小?

编辑:我还尝试评论 bottomView?.clipsToBounds = true

最佳答案

覆盖 super View 的 setFrame:setBounds:(如果它们是普通 UIView 的子类),添加断点,然后查看堆栈跟踪以找出导致它们调整大小的原因。

关于ios - UILabel 调整其 SuperView 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30685435/

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