【问题标题】:How to autoresize UIToolBar?如何自动调整 UIToolBar 的大小?
【发布时间】:2014-10-28 00:22:04
【问题描述】:

我希望工具栏自动出现在屏幕底部,并且我希望它调整宽度以便从 iPhone 调整到 iPad。下面的代码生成了一个静态 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;


}

【问题讨论】:

    标签: ios xcode uitoolbar


    【解决方案1】:

    您正在使用常量值设置它的框架。但是,此时您的视图框架已经设置好,并且不会根据您的 autoresizingMask 进行更改。您应该改用视图的 bounds 属性并执行以下操作:

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

    另一种选择是为您的视图定义约束。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      • 2019-07-03
      • 2013-09-28
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多