簡體   English   中英

自動調整大小不適用於以編程方式添加的UIView

[英]Autoresizing not working for UIView added programatically

我已經按照此SO鏈接進行了自動調整大小 ,但是自動調整大小不起作用。
如何通過自動調整大小以編程方式設置框架?

我已經為iPhone 4設置了框架

UIView *box = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 120)];
[box setBackgroundColor:[UIColor redColor]];

[box setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[box setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[box setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[box setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[box setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];

[self.view addSubview:box];

但它不適用於iPad或iPhone 6

首先,根據公開建議,您應該使用約束。

將自動調整大小的蒙版添加到視圖后,添加它。

UIView *box = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 120)];
[box setBackgroundColor:[UIColor redColor]];
[self.view addSubview:box];

[box setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[box setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[box setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[box setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[box setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];

如果我理解您的問題正確,那么您正在嘗試將紅色視圖設置為高度= 120

添加說明

您可以使用約束來實現:

        UIView *box = [[UIView alloc] init];
// Prevent creating constraints from masks automatically
        box.translatesAutoresizingMaskIntoConstraints = NO;
        box.backgroundColor = [UIColor redColor];
        [self.view addSubview:box];

// Define metrics (constants) which will be used to create constraints.
// Key @"boxSize" - name which will be used in constraints, Value - constant
        NSDictionary *metrics = @{@"boxSize" : @(120)};

// Define views that will participate in auto layout constraints.
// Key @"readBox" - name which will be used in constraints, Value - real UIView object
        NSDictionary *views = @{ @"redBox" : box };


// Here we create constraints. For Vertical, and for Horizontal

// I'm using Visual language format (you can find it in Apple Documentation
// In a few words:
// H:|-0-[redBox]-0-|
// "H" - means horizontal
// "|" - short cut for parent view (in our case it is UIViewController.view) 
// "[redBox]" - view name from view's dictionary
// "-0-" - gap between views (you could set number), in our case it is "|" and "[redBox]"
// "[redBox(boxSize)]" - means that view (redBox) size should be qual to "boxSize" from metrics' dictionary 


        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[redBox]-0-|" options:NSLayoutFormatAlignAllCenterY metrics:metrics views:views]];

        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[redBox(boxSize)]" options:NSLayoutFormatAlignAllCenterY metrics:metrics views:views]];

蘋果資料

您正在使用框架(0, 0, 320, 120) 0,0,320,120)設置UIView。 該框架將適合iPhone 4屏幕,因為手機屏幕寬度為320像素。 但是當您在iPhone 6 / 6s中運行代碼時,您無法期望得到相同的結果。 設置自動調整大小將無法解決此問題。 您需要為此使用約束/自動布局。

Autoresizing masks describe how a subview will resize or move when its superview is resized.

因此,添加此視圖后,如果更改手機方向,則會相應地調整視圖的大小。 但是您需要首先根據超級視圖設置框架。 您可以動態設置寬度,例如: (0, 0, self.view.frame.size.width, 120)

暫無
暫無

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

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