Reputation: 9893
This may sound a newbie question, however I'm new to iOS dev,
I've UIWebView and UITableView on my iPad view.
In shouldAutorotateToInterfaceOrientation
I resize them for nice look like this.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
self.mTextView.frame = f;
f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
self.mTableView.frame = f;
}
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
self.mTextView.frame = f;
f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
self.mTableView.frame = f;
}
return YES;
}
Now the question: For the first load the table view is drawn with correct sizes but when I switch to portrait mode then back to landscape mode the UITableView becomes wider than specified by frame. So why this happens and how to fix this ?
PS. I've tried also to set the frames for all cells in the same method didn't help.
PSS. This happens only on device, on simulator UITableView is displayed correctly
Upvotes: 1
Views: 6867
Reputation: 5421
You can change the size of UITableView in both portrait and landscape modes. UITableView change size automatically in a ratio with respect to previous orientation mode . So you can fix desired size for both modes.
Hope this work for you.
Upvotes: 2
Reputation: 431
you use AutoResizing
for this no need to do coding for frame..
http://developer.apple.com/library/ios/#documentation/windowsviews/conceptual/viewpg_iphoneos/CreatingViews/CreatingViews.html
Upvotes: 0
Reputation: 779
The simplest way is you can make 2 diffrent view for the portrait and landscape mode so whenever mode changes you can easily change the view. Like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
self.view = self.portraitView;
else
self.view = self.landscapeView;
[tblScore reloadData];
return YES;
}
Upvotes: 0
Reputation: 3437
iOS calls this function just one time! For doing this work nice as you expect, you must add this line into the (void)viewDidLoad
function:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
And making (void)orientationChanged:(id)object
function:
- (void)orientationChanged:(id)object{
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication] statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
// some works
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
// another works
}
}
I hope it be useful for you!
Upvotes: 2
Reputation: 2822
shouldAutorotateToInterfaceOrientation:
only returns a boolean value indicating whether the view controller supports the specified orientation, you should not do any UI transformation in this method, instead do everything in willAnimateRotationToInterfaceOrientation:duration:
.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
self.mTextView.frame = f;
f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
self.mTableView.frame = f;
}
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
self.mTextView.frame = f;
f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
self.mTableView.frame = f;
}
}
Have a look at the documentation for UIViewController, it's well explained.
Upvotes: 4