snippet code selengkapnya adalah seperti di bawah ini.
//rotation handling
- (void)canRotate{}
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification{
[self adjustViewsForOrientation:[[UIDevice currentDevice] orientation]];
}
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
// if (orientation == UIInterfaceOrientationPortrait)
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
landscape = NO;
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[self.tableView reloadData];
//load the portrait view
NSLog(@"portraid");
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self.navigationController.navigationBar setHidden:NO];
}
// else if (orientation == UIInterfaceOrientationLandscapeLeft)
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
landscape = YES;
//harus di tambahin ini karena parentnya uiviewcontroller bukan uitableviewcontroller jadi tidak otomatis
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, 480, 320);
[self.tableView layoutSubviews];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[self.tableView reloadData];
//load the landscape view
NSLog(@"landscape");
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self.navigationController.navigationBar setHidden:NO];
}
}
jangan lupa untuk reloaddata tableview anda setelah melakukan perubahan.
Jika tableview anda masih belum mau berubah juga, silahkan coba trik terakhir ini.
tambahkan code perubahan size tableview pada method viewForHeaderInSection. Sehingga menjadi seperti ini.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header;// = [[UIView alloc] initWithFrame:(CGRect){0,0,320,67}];
if (landscape) {
NSLog(@"header landscape called");
header = [[UIView alloc] initWithFrame:(CGRect){0,0,480,47}];
[header setBackgroundColor:[UIColor colorWithRed:0.922 green:0.922 blue:0.922 alpha:1] /*#ebebeb*/];
//taruh sini biar mau beruah ukurannya
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, 480, 320);
[self.tableView layoutSubviews];
}else{ ........ }
Selamat mencoba.
No comments:
Post a Comment