2016-08-05 53 views
0

我跟着this SO link for autoresizing,但Autoresizing不工作。
如何使用自動調整以編程方式設置框架?自動調整不工作UIView編程添加

我已經設置框架爲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

+0

爲什麼不使用約束而不是調整面具大小? – Konstantin

+0

因爲我沒有使用故事板UI.i編程使用@Konstantin –

+0

無論您如何創建您的用戶界面,您仍然可以使用代碼約束。 – Konstantin

回答

0

第一件事:按照開放的建議,你應該使用的限制。

添加自動調整掩碼後添加到查看。

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]; 
+0

感謝您的回覆Kaushal,我已經試過這段代碼,但是不適合我 –

+0

你的面具枚舉不正確,如果你想固定寬度,高度不要使用UIViewAutoresizingFlexibleHeight,UIViewAutoresizingFlexibleWidth。在目前的情況下,你的框架將變爲零。 – kaushal

0

如果我理解你的問題正確,您正在嘗試設置與高度頂紅色視圖= 120

ADD說明

你可以用約束實現它:

 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]]; 

Apple Documentation

+0

我已經使用你的代碼,這是工作。可以請你更清楚地解釋,你做了什麼正好,我不明白..謝謝你 –

+0

你有設置框架? –

+2

我更新了答案並試圖向您解釋。你可以在下面提供的鏈接中找到更多的細節 – Konstantin

0

您正在設置一個框架爲(0, 0, 320, 120)的UIView。這個框架將適合iPhone 4屏幕,因爲手機屏幕寬度是320像素。但是當你在iPhone 6/6s中運行代碼時,你不能期待這一點。設置Autoresizing不會處理這個問題。你需要使用約束/自動佈局。

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

所以加入這個觀點後,如果您改變手機方向,這將相應地調整到位的觀點。但是您需要先根據超級視圖設置框架。您可以動態設置寬度,如:(0, 0, self.view.frame.size.width, 120)

相關問題