我有一个tableView,我必须根据设备是纵向还是横向来定义不同单元格的高度,但似乎此方法无法识别设备方向。此方法不起作用:
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice]orientation];//here doesn't recognize the orientation
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation== UIInterfaceOrientationPortraitUpsideDown) {
self.tableView.rowHeight =33.33333;
}
else if (deviceOrientation== UIInterfaceOrientationLandscapeRight || deviceOrientation==UIInterfaceOrientationLandscapeLeft){
self.tableView.rowHeight = 16.333333;
}
}
else {
if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation== UIInterfaceOrientationPortraitUpsideDown){
self.tableView.rowHeight = 66.66666;
}else if (deviceOrientation== UIInterfaceOrientationLandscapeRight || deviceOrientation==UIInterfaceOrientationLandscapeLeft){
self.tableView.rowHeight = 33.333333;
}
}
return self.tableView.rowHeight;
}
答案 0 :(得分:0)
使用:
UIDeviceOrientation deviceOrientation = [UIApplication sharedApplication].statusBarOrientation;
识别状态栏方向,而不是:
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice]orientation];
答案 1 :(得分:0)
使用单例类方法 UIDeviceOrientation deviceOrientation = [UIApplication sharedApplication] .statusBarOrientation];