如何自动调整UIToolBar?
问题描述:
我希望工具栏自动出现在屏幕的底部,我希望它调整宽度,以便从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;
}
答
您正在使用常数值设置它的帧。但是,您的视图的框架已经设置在这一点上,并且它不会根据您的autoresizingMask进行更改。您应该使用视图边界属性,而不是做一些事情,如:
toolbar.frame = CGRectMake(self.view.bounds.origin.x, 0, self.view.bounds.size.width, 50);
另一种选择是定义视图约束。