Reputation: 2522
So I have an application which I'm trying to optimize for all the orientations. But I'm having a problem with table views and autorotation. This is how it looks when in Portrait:
Now, when I rotate the device, it looks like this:
But as SOON as I touch the table it fixes itself and looks like this:
So, how can I make it happen automatically when the view is rotated and not wait for the user to tap on the table? Thanks!
I tried with the following code with no success:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation {
[self.tableView reloadData];
}
Upvotes: 1
Views: 1531
Reputation: 145
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
return yes is needed of autorotaion .
Upvotes: 0
Reputation: 104082
I don't know if this gives exactly the same look as you have (I can't really tell from your pictures). The cells fade into black at the bottom of the table. This code is in a UIViewController, with a table view added as a subview. The table view was given a blue background color. This worked perfectly on rotation.
-(void)viewWillLayoutSubviews {
self.maskGradient.frame = self.view.bounds;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.maskGradient = [CAGradientLayer layer];
self.maskGradient.frame = self.view.bounds;
self.maskGradient.colors = @[(id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor],(id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor],(id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:.2] CGColor]];
self.maskGradient.locations = @[@0.0f,@0.9f,@1.00f];
self.view.layer.mask = self.maskGradient;
// other code to populate the table
[self.tableView reloadData];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor clearColor];
}
Upvotes: 0
Reputation: 2522
So the problem was not with the table, but with a mask layer I was using. It looks like CALayers don't support autoresizing, so I'm using the following code:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation {
maskLayer.frame = self.maskView.bounds;
}
It doesn't animate while rotating. It finishes rotating, and THEN animates the mask. It's weird, but it works for me. Should anyone have a better solution, I'll remove my answer and give it to someone else.
Upvotes: 1