I want to resize my toolbar
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 l开发者_StackOverflow社区andscape 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.
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?
精彩评论