我正在使用标签栏 Controller 并处理登录和注销功能。下面是在 iPad 横屏模式下调整 tableViewCell 的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"collabId";
emptyCell *cell = (emptyCell *)[self.tableViewJoined dequeueReusableCellWithIdentifier:cellId];
if(!cell)
{
NSArray *nib;
UIButton *buttonList;
UIButton *buttonAttachment;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
nib = [[NSBundle mainBundle] loadNibNamed:@"emptyCell" owner:self options:nil];
buttonList = [[UIButton alloc] initWithFrame:CGRectMake(199, 0, 30, 25)];
buttonAttachment = [[UIButton alloc] initWithFrame:CGRectMake(290, 0, 30, 25)];
}
else
{
if(UI_USER_INTERFACE_IDIOM() == UIDeviceOrientationPortrait)
{
nib = [[NSBundle mainBundle] loadNibNamed:@"emptyCell_iPad" owner:self options:nil];
buttonList = [[UIButton alloc] initWithFrame:CGRectMake(452, 0, 62, 25)];
buttonAttachment = [[UIButton alloc] initWithFrame:CGRectMake(650, 0, 98, 25)];
}
else if(UI_USER_INTERFACE_IDIOM() == UIDeviceOrientationLandscapeLeft || UI_USER_INTERFACE_IDIOM() == UIDeviceOrientationLandscapeRight)
{
nib = [[NSBundle mainBundle] loadNibNamed:@"emptyCell_iPad_Landscape" owner:self options:nil];
buttonList = [[UIButton alloc] initWithFrame:CGRectMake(591, 0, 81, 24)];
buttonAttachment = [[UIButton alloc] initWithFrame:CGRectMake(850, 0, 128, 24)];
}
}
for (id object in nib)
{
if([object isKindOfClass:[emptyCell class]])
{
cell = (emptyCell *)object;
break;
}
}
[buttonList addTarget:self action:@selector(buttonListClicked:) forControlEvents:UIControlEventTouchUpInside];
[buttonList setTag:indexPath.row];
[cell.contentView addSubview:buttonList];
[buttonAttachment addTarget:self action:@selector(buttonAttachmentClicked:) forControlEvents:UIControlEventTouchUpInside];
[buttonAttachment setTag:indexPath.row];
[cell.contentView addSubview:buttonAttachment];
cell = [nib objectAtIndex:0];
SaveCollaboration *saveCollab = [mutableArray objectAtIndex:indexPath.row];
cell.name.text = saveCollab.name;
cell.project.text = saveCollab.project;
cell.date.text = saveCollab.date;
cell.invites.text = [NSString stringWithFormat:@"%d", saveCollab.invites];
cell.docsCount.text = [NSString stringWithFormat:@"%d", saveCollab.docsCount];
}
return cell;
}
我注意到的一件事是,对于登录用户的第一页,每当我更改其方向时,都会调用以下 AppDelegate 方法三次。其他页面不给下面的方法log三次。
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
NSLog(@"Orientation changed"); //Gets invoked thrice. Is this the reason why landscape cells don't get invoked?
return UIInterfaceOrientationMaskAllButUpsideDown;
}
我在
nib = [[NSBundle mainBundle] loadNibNamed:@"emptyCell_iPad_Landscape" owner:self options:nil];
但它从未被调用过。
当用户成功登录时,您可以引用以下委托(delegate)方法。
我检查了
emptyCell_iPad_Landscape
的标识符 Nib 和自动布局未选中。- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
BOOL getFlag = delegate.flagToCheckLogin;
if(!getFlag)
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:Nil];
viewController.delegate = self;
[self presentViewController:viewController animated:NO completion:nil];
}
else
{
ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:Nil];
viewController.delegate = self;
[self presentViewController:viewController animated:NO completion:nil];
}
}
}
登录类中有以下方法..
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate
{
return YES;
}
最佳答案
我相信你在滥用 UI_USER_INTERFACE_IDIOM()
功能。根据Apple Docs , UI_USER_INTERFACE_IDIOM()
“返回当前设备支持的接口(interface)习惯用法。”通常,此函数用于确定设备是 iPhone 还是 iPad,但由于 UIDeviceOrientationPortrait
事实上,正如您的应用程序委托(delegate)中所指定的那样,这种情况总是正确的,这是有道理的。 (这也可能是为什么 supportedInterfaceOrientationsForWindow:
在您的 appDelegate 中,正如您所说,“调用三次”......因为您多次请求 UI_USER_INTERFACE_IDIOM()
)。
而不是使用 UI_USER_INTERFACE_IDIOM()
== ... ,您可以:
(1) 使用[[UIDevice currentDevice] orientation]
检测设备的当前方向:
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
…
} else if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
}
或者
(2) 使用
[UIApplication sharedApplication].statusBarOrientation
获取界面的当前方向:UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIDeviceOrientationPortrait) {
…
} else if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
}
请注意,解决方案 #1 和 #2 并不总是返回相同的结果。 #1 返回设备的当前方向。 #2 返回界面的当前方向。因此,根据您的需要,一种方法对您来说可能比另一种更可靠……我建议在您的情况下使用方法#2。
关于ios - 无法在 ios 中调整横向 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20854969/