簡體   English   中英

AutoLayout在UILabel上不起作用

[英]AutoLayout Not working on UILabel

我在標簽欄控制器中有一個UIImageView和UILabel的視圖。 我將UILabel定位為使文本將出現在UIImageView的心臟中。 圖像視圖占據了整個視圖。 我正在使用AutoLayout來放置所有內容,並對其進行設置以拉伸圖像以在旋轉時填充整個屏幕,但是我遇到了將UILabel放置在所需位置的問題。

我曾嘗試將TopLab固定到SuperView,但是這會導致UILabel太低,而固定到底部會使UILabel在旋轉時無法顯示。 這是顯示第一件事的圖像。

更新:我嘗試了以下代碼,但無濟於事:

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        NSLog(@"Landscape left");
 label1.frame = CGRectMake(29, 20, 509, 142);
        [label1 setFrame:CGRectMake(29, 20, 509, 142)];

    } else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"Landscape right");
         label1.frame = CGRectMake(29, 20, 509, 142);
        [label1 setFrame:CGRectMake(29, 20, 509, 142)];

    } else if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        NSLog(@"Portrait");
        [label1 setFrame:CGRectMake(29, 77, 61, 142)];
    }
}

在此處輸入圖片說明在此處輸入圖片說明

使用自動布局時,絕對不要直接設置框架。 所有移動和調整大小都應通過約束來完成。 您可以使用乘數和常數在代碼中設置標簽的位置,這樣視圖將自動更改其位置,而無需檢查方向。 舉例來說,如果您有這個

NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTop relatedBy:0 toItem:self.view attribute:NSLayoutAttributeBottom multiplier:0.2 constant:-60];

子視圖的位置在縱向上距頂部53.6點,在橫向上距頂部4點。 進行計算以找出要插入的數字有點麻煩,所以我在NSLayoutConstraint上寫了一個類別來為我做這件事。 因此,類別具有一種如下所示的方法:

+(NSLayoutConstraint *)topConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
    CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.height - superview.bounds.size.width);
    CGFloat constant = pValue - (superview.bounds.size.height * multiplier);
    NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeBottom multiplier:multiplier constant:constant];
    return con;
}

您可以非常簡單地使用它,如下所示:

[self.view addConstraint:[NSLayoutConstraint topConstraintForView:self.label viewAttribute:NSLayoutAttributeTop superview:self.view portraitValue:100 landscapeValue:50]];

這將計算乘數和常數的正確值,並正確放置視圖。 在IB中,您需要對標簽設置最高限制,然后在添加此標簽之前將其刪除。 您可以通過編輯約束,然后選中標題為“在構建時刪除占位符”的框來實現。

如果要在不使用類別的情況下執行此操作,則可以稍作更改,然后將其添加到要設置約束的類中。 像這樣:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addConstraint:[self topConstraintForView:self.label viewAttribute:NSLayoutAttributeTop superview:self.view portraitValue:100 landscapeValue:50]];
}



-(NSLayoutConstraint *)topConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
    CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.height - superview.bounds.size.width);
    CGFloat constant = pValue - (superview.bounds.size.height * multiplier);
    NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeBottom multiplier:multiplier constant:constant];
    return con;
}

來過這里無休止的時間后,我建議您使用代碼按照旋轉方法的要求放置標簽。 創建IBOutlet,然后在設備旋轉時將框架調整到兩個位置之一。

這樣一來,您很有可能不得不將文本尺寸縮小,以便動態地適合心臟。 同樣,如果內心不是自己的看法,那就讓自己成為自己的看法。 這將有助於自動布局了解如何更好地定義自身。 也是為了幫助您更好地了解這是一個很棒的資源http://nsscreencast.com/episodes/35-autolayout-fun

祝好運。

簡單的步驟,只需使用與您的需求相關的約束即可。

在此處輸入圖片說明

然后根據您的需要設置Relation> =或<=或== 還要在界面生成器中設置行數。

在此處輸入圖片說明

參考: https : //medium.com/@elgoni/ios-7-auto-layout-3e50cd1d1278

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM