2015-06-06 61 views
0

我有以下視圖,其包含一個UILabelUILabel調整其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在運行時調整大小。這意味着綠底視圖的尺寸也會增加。

下面是結果時,標籤上有兩個和三個行:

enter image description here

看來,UILabel重新調整其超強的視圖。

請注意,我不使用AutoLayout。

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

我怎樣才能防止UILabel從調整它的父?

編輯:我也想發表評論bottomView?.clipsToBounds = true

+0

標籤被放置在使用自動佈局的視圖層次結構內嗎? – Fogmeister

+0

您是否已使用4,5或更多行文字進行驗證? bottomView的高度是否在不斷增加? –

+0

發佈您自己的'layoutSubviews'' – Bannings

回答

0

覆蓋setFrame:和超級視圖的setBounds:(子類,如果他們是普通的UIView S),添加斷點,看堆棧跟蹤找出是什麼導致他們調整大小。

+0

setFrame和setBounds在哪裏? – confile

+0

@confile你必須重寫這些方法 – rounak

+0

你在說什麼超級視圖? – confile

0

無需在標籤上設置autoResizingMask,只需設置框架,它就會自動居中。當然,您可以相應地爲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) 

}