【问题标题】:UILabel resizes its SuperView?UILabel 调整其 SuperView 的大小?
【发布时间】:2015-06-06 17:07:08
【问题描述】:

我有以下包含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 在运行时调整大小。这意味着绿色底视图的大小也会增加。

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

UILabel 似乎调整了其超级视图的大小。

请注意,我不使用 AutoLayout。

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

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

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

【问题讨论】:

  • 标签是否放置在使用自动布局的视图层次结构中?
  • 您是否使用 4 行、5 行或更多行文本进行了验证? bottomView 的高度是不是一直在增加?
  • 发布您的layoutSubviewsself
  • 评论此行并检查。 bottomView?.clipsToBounds = true
  • @Bannings 我发布了 layoutSubviews

标签: ios swift uilabel


【解决方案1】:

覆盖超级视图的setFrame:setBounds:(如果它们是普通的UIViews,则为子类),添加断点,并查看堆栈跟踪以找出导致它们调整大小的原因。

【讨论】:

  • setFrame 和 setBounds 在哪里?
  • @confile 你必须重写这些方法
  • 你说的是哪个超级视图?
  • 我用的是 Swift 没有 setFrame 和 setBounds
  • 我试过这个class BottomView : UIView { override func setFrame(frame: CGRect) { super.frame = frame } }但它会引发错误。
【解决方案2】:

标签上不需要设置autoResizingMask,只要设置frame,它就会自动居中。当然,您可以相应地设置 UILabel 的插图。我在下面添加了测试代码仅供参考:

 override func viewDidLayoutSubviews() {
    addTestView(CGRectMake(0, 200, view.bounds.width, 50), labelStr: "I am a short testing label")
    addTestView(CGRectMake(0, 260, view.bounds.width, 50), labelStr: "I am a very longlonglonglonglonglonglong testing label")
     addTestView(CGRectMake(0, 320, view.bounds.width, 50), labelStr: "I am a very longlonglonglonglonglonglonglonglonglonglonglong testing label. Will be truncated")
}

func addTestView(frame:CGRect, labelStr: String){
    let bottomView = UIView(frame:frame)
    bottomView.backgroundColor = UIColor.greenColor()
    bottomView.autoresizingMask = UIViewAutoresizing.FlexibleWidth
    bottomView.clipsToBounds = true
    view.addSubview(bottomView)

    var label = UILabel(frame: bottomView.bounds)
    label.textAlignment = NSTextAlignment.Left
    label.numberOfLines = 0
    label.text = labelStr
    bottomView.addSubview(label)

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    相关资源
    最近更新 更多