我想在方向更改时调整我的工具栏的大小。 但我遇到了一个难题。我想调整我的工具栏的大小
我有两个视图控制器。 第一个视图控制器有一个按钮。当此按钮触摸时,第二个视图将出现在显示屏上。这些视图控制器属于导航控制器。
第一个视图控制器使导航栏显示。而第二个视图控制器使导航栏隐藏。
在第一视图控制器
- (void) viewWillAppear: (BOOL)animated {
[ super viewWillAppear: animated ];
[ [ [ self navigationController ] navigationBar ] setHidden: NO ];
UIButton *nextButton;
nextButton = [ UIButton buttonWithType: UIButtonTypeRoundedRect ];
[ nextButton setFrame: CGRectMake(50.0, 50.0, 200.0, 150.0) ];
[ nextButton setTag: 100 ];
[ nextButton addTarget: self action: @selector(touchedNextButton:) forControlEvents: UIControlEventTouchUpInside ];
[ [ self view ] addSubview: nextButton ];
}
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
BOOL iResult;
iResult = ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight));
return iResult;
}在第二视图控制器
- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation {
UIInterfaceOrientation orientation = [ [ UIDevice currentDevice ] orientation ];
UIButton *button = (UIButton *)[ [ self view ] viewWithTag: 100 ];
switch (orientation) {
case UIInterfaceOrientationPortrait:
[ button setFrame: CGRectMake(50.0, 50.0, 200.0, 150.0) ];
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
[ button setFrame: CGRectMake(50.0, 50.0, 200.0, 150.0) ];
break;
default:
break;
}
}
重要的方法
- (void) viewDidLoad {
[ [ [ self navigationController ] navigationBar ] setHidden: YES ];
UIImage *imageBackArrow_iPhone = [ UIImage nonCachedImageNamed: @"backArrow_iPhone.png" ];
NSMutableArray *toolbarItems = [ [ NSMutableArray alloc ] initWithCapacity: 5 ];
[ toolbarItems addObject: [ [ [ UIBarButtonItem alloc ] initWithImage: imageBackArrow_iPhone style: UIBarButtonItemStylePlain target: self
action: @selector(touchedBackArrowButton) ] autorelease ] ];
UIToolbar *toolbar = [ [ UIToolbar alloc ] initWithFrame: CGRectMake(0.0, 416.0, 320.0, 44.0) ];
[ toolbar setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin ];
[ toolbar setItems: toolbarItems ];
[ toolbarItems release ];
[ [ self view ] addSubview: toolbar ];
[ toolbar release ];
}
}
其余我来说很重要的方法第二视图控制器中的方法类似于第一视图控制器。
在第一视图控制器开始对肖像模式后,我触摸的下一个按钮。改变方向工作正常。 (第一个截图)
但是从第一个视图控制器的纵向模式开始,我改变为横向模式。 然后我触摸下一个按钮,这使得奇怪的布局。 (第二个截图)
我不知道是什么导致了这个问题。
请让我知道您的经验。你的建议会让我清醒。 谢谢您的阅读。
我正在使用自动调整掩码。 [工具栏setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin]; – MonsterK 2011-05-21 07:05:35