我想调整工具栏的大小
我想在方向改变时调整工具栏的大小。 但我遇到了一个难题。
我有两个视图控制器。 第一个视图控制器有一个按钮。当触摸此按钮时,显示屏上将出现第二个视图。这些视图控制器属于导航控制器。
第一个视图控制器使导航栏显示。第二个视图控制器使导航栏隐藏。
第一个视图控制器中的重要方法
- ( 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 ];
}
}
第二个视图控制器中的其余方法与第一个视图控制器类似。
纵向模式后,我触摸了下一步按钮。改变方向效果很好。 (第一个屏幕截图)
但是从第一个视图控制器中的纵向模式开始,我更改为横向模式。 然后我触摸了下一个按钮,这造成了奇怪的布局。 (第二个屏幕截图)
我不知道是什么造成了这个问题。
请让我知道您的经历。你的建议会让我清醒。 感谢您的阅读。
I want to resize my toolbar when orientation changes.
But I have faced a difficult problem.
I have two view controllers.
The first view controller have a button. When this button touches, the second view will appear on display. These view controllers belong to a navigation controller.
The first view controller make navigation bar show. And second view controller make navigation bar hide.
important methods in the first view controller
- ( 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;
}
}
important methods in the second view controller
- ( 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 ];
}
}
The rest methods in second view controller are similar to the first view contoller.
After starting on portrait mode in the first view controller, I touched the next button. Changing Orientations work fine.
(The first screen shot)
But starting on portrait mode in the first view controller, I changed to landscape mode.
Then I touched the next button, this made weird layout.
(The second screen shot)
I don't know what makes this problem.
Please let me know your experiences. Your advices will make me awake.
Thank you for your reading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该代码看起来不太可能......但是
您也可以尝试在视图控制器的 viewWillAppear 方法中调整内容的大小。
这可能会有所帮助。
The code looks very unlikely.... but
you can try resizing stuff in your viewWillAppear Method of your viewcontroller as well.
This could help.
为什么不直接使用自动调整大小蒙版呢?
Why don't you just use autoresizing masks?