У меня есть следующий вид, который содержит UILabel
:UILabel изменяет размер своего SuperView?
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?
Edit: Я также попытался прокомментировать bottomView?.clipsToBounds = true
Является ли метка помещенной внутри иерархии представлений, которая использует автоматическое расположение? – Fogmeister
Вы проверили 4, 5 или более строк текста? Увеличивается ли высота bottomView? –
Опубликуйте свои 'layoutSubviews'' self' – Bannings