无法在ios中调整横向视图

时间:2013-12-31 09:25:03

标签: ios uitableview orientation

我正在使用标签栏控制器并处理登录和注销功能。以下是在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方法三次。其他页面没有给出以下方法三次。

- (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];

但它从未被调用过。 当用户成功登录时,您可以参考以下委托方法。 我检查了emptyCell_iPad_Landscape nib的标识符,并且未选中autolayout。

- (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;
}

2 个答案:

答案 0 :(得分:3)

我相信您滥用UI_USER_INTERFACE_IDIOM()功能。根据{{​​3}},UI_USER_INTERFACE_IDIOM()"返回当前设备支持的界面惯用法。"通常,此功能用于确定设备是iPhone还是iPad,但由于UIDeviceOrientationPortrait实际上支持应用委托中指定的,因此这种情况始终是正确的。 (这也可能是你appDelegate中supportedInterfaceOrientationsForWindow:的原因,正如你所说,"调用了三次" ...因为你多次请求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。

答案 1 :(得分:0)

在检查cellForRowAtIndexPath中的if(!cell)之前替换此代码。现在断点将被调用。

static NSString *cellId = nil;
BOOL isPortraitOrientation = UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]);
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        cellId = @"collabId-iPhone”;
    }
    else {
    if(isPortraitOrientation)
      {
       cellId = @"collabId-iPadPortrait”;
      }
    else
      {
       cellId = @"collabId-iPadLandscape”;
     }  
    }
    emptyCell *cell = (emptyCell *)[self.tableViewJoined dequeueReusableCellWithIdentifier:cellId];

祝你好运!!!