[英]Autoresizing subviews in iPhone programmatically
我創建了一個UIViewController和一個帶有兩個UIButton的UIView。
self.view = [[UIView alloc]initWithFrame:CGRectMake( 0,0,320,480)];
self.view.autoresizesSubviews = YES;
self.view.backgroundColor = [UIColor redColor];
UIView *view;
view = [[UIVew alloc]initWithFrame:CGRectMake(0,0,320,480)];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIButton *button1;
button.frame = CGRectMake(100,130,120,50);
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
[controller addSubview:view];
// […]
- (void)buttonAction:(id)sender
{
// I have created another view for a purpose
[view removeFromSuperview];
UIView *view_obj;
view_obj.backgroundColor = [UIColor greenColor];
view_obj.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:view_obj];
}
問題:當我將方向更改為橫向時,具有按鈕的視圖會被調整大小,但是當我單擊按鈕時,帶有blueColor的視圖會顯示而不會自動調整大小。 在這里我可以看到一半是redColor的根視圖,一半是greenColor的子視圖。我還覆蓋了ShouldAutoRotate方法並以編程方式完成了該項目。 請回復我。
在Interface Builder中,選擇視圖對象,然后在檢查器窗口的“ View Attributes”窗格中,確保已選擇狀態欄和“ Navigation Bar”的“ Top Bar”(假設您的應用程序具有導航欄)。 您將在“模擬用戶界面元素”部分的頂部附近看到這些內容。 這通知應用程序在確定UIViewController視圖的大小時始終為這些留出空間。
嘗試研究這個問題。
要么
請參見下面的代碼從這個問題 。
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
if (interfaceOrientation == UIInterfaceOrientationPortrait
|| interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
button1.frame = CGRectMake(100, 33, 226, 275);
button2.frame = CGRectMake(440, 33, 226, 275);
button3.frame = CGRectMake(100, 360, 226, 275);
button4.frame = CGRectMake(440, 360, 226, 275);
button5.frame = CGRectMake(100, 693, 226, 275);
button6.frame = CGRectMake(440, 693, 226, 275);
}
else
{
button1.frame = CGRectMake(100, 50, 226, 275);
button2.frame = CGRectMake(400, 50, 226, 275);
button3.frame = CGRectMake(700, 50, 226, 275);
button4.frame = CGRectMake(100, 400, 226, 275);
button5.frame = CGRectMake(400, 400, 226, 275);
button6.frame = CGRectMake(700, 400, 226, 275);
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation || UIInterfaceOrientationPortraitUpsideDown);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.