2014-03-25 23 views
0

我正在嘗試向UIScrollView添加標籤。我添加了頂部約束和主要約束。 標籤沒有出現。如何以編程方式添加標籤並使用自動佈局進行編程定位

這是我的代碼。

self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; 
    self.label.translatesAutoresizingMaskIntoConstraints = NO; 
    self.label.backgroundColor = [UIColor clearColor]; 
    self.label.textAlignment = NSTextAlignmentCenter; 
    self.label.textColor=[UIColor whiteColor]; 
    self.label.text = @"Hello"; 
    [self.imageCarouselScrollView addSubview:self.label]; 
    [self bringSubviewToFront:self.label]; 
    self.titleLabelTopConstraint = [NSLayoutConstraint 
            constraintWithItem:self 
            attribute:NSLayoutAttributeTop 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.imageCarouselScrollView 
            attribute:NSLayoutAttributeBottom 
            multiplier:1.0 
            constant:20]; 
    NSLayoutConstraint *titleLeadingConstraint = [NSLayoutConstraint 
                constraintWithItem:self 
                attribute:NSLayoutAttributeLeading 
                relatedBy:NSLayoutRelationEqual 
                toItem:self.imageCarouselScrollView 
                attribute:NSLayoutAttributeTrailing 
                multiplier:1.0 
                constant:20]; 
    [self addConstraints:@[self.titleLabelTopConstraint,titleLeadingConstraint]]; 
+0

你有沒有嘗試過,看看是否'UILabel'顯示了出來加入的約束?而且,不要試圖搞笑..你的意見背景顏色不是設置爲白色的嗎? –

回答

0

你必須包括滾動視圖的約束向右(對於水平滾動)或底部(垂直滾動),以便它可以推斷出所述內容的尺寸。

0

我嘗試註釋代碼:

self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; //if 'translatesAutoresizingMaskIntoConstraints=NO' frame not work work anymore. Use CGRectZero instead 
    self.label.translatesAutoresizingMaskIntoConstraints = NO; 
    self.label.backgroundColor = [UIColor clearColor]; 
    self.label.textAlignment = NSTextAlignmentCenter; 
    self.label.textColor=[UIColor whiteColor]; 
    self.label.text = @"Hello"; 
    [self.imageCarouselScrollView addSubview:self.label]; //Your self.label added on 'self.imageCarouselScrollView' but your constraints added on self 
    [self bringSubviewToFront:self.label]; // ??? Why? 
    self.titleLabelTopConstraint = [NSLayoutConstraint 
      constraintWithItem:self 
        attribute:NSLayoutAttributeTop 
        relatedBy:NSLayoutRelationEqual 
         toItem:self.imageCarouselScrollView 
        attribute:NSLayoutAttributeBottom 
        multiplier:1.0 
         constant:20]; //it constrain means: "self view top edge equal to self.imageCarouselScrollView bottom + offset 20 " 
    NSLayoutConstraint *titleLeadingConstraint = [NSLayoutConstraint 
      constraintWithItem:self 
        attribute:NSLayoutAttributeLeading 
        relatedBy:NSLayoutRelationEqual 
         toItem:self.imageCarouselScrollView 
        attribute:NSLayoutAttributeTrailing 
        multiplier:1.0 
         constant:20];//it constrain means: "self view Leading edge equal to self.imageCarouselScrollView Trailing + offset 20 " 
    [self addConstraints:@[self.titleLabelTopConstraint,titleLeadingConstraint]]; 

,現在我有幾個問題:對於self.label

  1. 爲什麼限制?因爲在翻譯完成之後MakesMaskIntoConstraints = no,它等於0;你再也看不到標籤了。
  2. 你有2個約束關於其他邊緣?
  3. 毫無疑問,議會,嘗試使用「constraintsWithVisualFormat:選擇:指標:觀點:」
+0

使用標籤只有兩個約束來確定其x值和y值,那麼其寬度和高度將基於其內容。我沒有真正嘗試過constraintWithVisualFormat。 – user1898829

+0

@ user1898829'sizeToFits'不適用於更多的自動佈局,學習'setContentHuggingPriority:forAxis:'和'setContentCompressionResistancePriority:forAxis:' – Bimawa

相關問題