2014-10-27 70 views
0

我希望工具欄自動出現在屏幕的底部,我希望它調整寬度,以便從iPhone調整到iPad。下面的代碼產生一個靜態的UIToolbar,它保持在相同的位置。如何讓工具欄出現在底部,以及如何根據屏幕尺寸自動調整寬度?如何自動調整UIToolBar?

- (UIView*)commomOverlay 
{ 


UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0,0,400,430)]; 


CGRect rect = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 44); /* 
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
{ 
    rect = CGRectMake(0,0,768,1004); 
} 
UIImageView *FrameImg = [[UIImageView alloc] initWithFrame:rect]; 
[FrameImg setImage:[UIImage imageNamed:@"newGraphicOverlay.png"]]; 
FrameImg.userInteractionEnabled = YES; 
[view addSubview:FrameImg]; 
[FrameImg release];*/ 

rect = CGRectMake(0, 0, 400, 50); 



UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame:rect]; 
[myToolBar setBarStyle:UIBarStyleBlackTranslucent]; 
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Finish" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelButtonPressed)]; 
UIBarButtonItem *flexiSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(myFunction)]; 
mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake (123,350,40,20)]; 
[mySwitch addTarget:self action:@selector(toggleFlash:) forControlEvents:UIControlEventAllTouchEvents]; 
UIBarButtonItem *switchBtn = [[UIBarButtonItem alloc] initWithCustomView:mySwitch]; 
//Order of how buttons appear or toolbar left to right. 
[myToolBar setItems:[NSArray arrayWithObjects: cancelButton, flexiSpace,switchBtn, nil] animated:YES]; 
myToolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ; 


[cancelButton release]; 
[flexiSpace release]; 
[switchBtn release]; 

[view addSubview:myToolBar]; 
return view; 


} 

回答

0

您正在使用常數值設置它的幀。但是,您的視圖的框架已經設置在這一點上,並且它不會根據您的autoresizingMask進行更改。您應該使用視圖邊界屬性,而不是做一些事情,如:

toolbar.frame = CGRectMake(self.view.bounds.origin.x, 0, self.view.bounds.size.width, 50); 

另一種選擇是定義視圖約束。