UITableView不会在设备方向改变时正确地改变它的大小



这听起来可能是个新手问题,但我是iOS开发新手,

我在iPad视图上有UIWebView和UITableView。在shouldAutorotateToInterfaceOrientation中,我调整了它们的大小,使其看起来像这样。

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {    
        if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
            CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
            self.mTextView.frame = f;
            f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
            self.mTableView.frame = f;
        }
        if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {     
            CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
            self.mTextView.frame = f;
            f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
            self.mTableView.frame = f;        
        }   
        return YES;
    }

现在问题是:对于第一次加载,表格视图绘制正确的大小,但当我切换到纵向模式,然后回到横向模式,UITableView变得比指定的帧更宽。那么为什么会发生这种情况以及如何解决这个问题呢?

p。我也试过用相同的方法为所有单元格设置帧,但没有帮助。

PSS。这只发生在设备上,在模拟器上UITableView被正确显示

shouldAutorotateToInterfaceOrientation:只返回一个布尔值,指示视图控制器是否支持指定的方向,你不应该在这个方法中做任何UI转换,而是在willAnimateRotationToInterfaceOrientation:duration:中做所有的事情。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{    
    if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
        CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
        self.mTextView.frame = f;
        f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
        self.mTableView.frame = f;
    }
    if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {     
        CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
        self.mTextView.frame = f;
        f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
        self.mTableView.frame = f;        
    }
}

看一下UIViewController的文档,它解释得很好

iOS只调用这个函数一次!为了像您期望的那样完成这项工作,您必须将这一行添加到(void)viewDidLoad函数中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

并使(void)orientationChanged:(id)object函数:

- (void)orientationChanged:(id)object{
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication] statusBarOrientation;
    if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
        // some works
    }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
        // another works
    }
}

我希望它对你有用!

你可以在纵向和横向模式下改变UITableView的大小。UITableView根据之前的方向模式自动按比例改变大小。因此,您可以为两种模式设置所需的大小。

希望这对你有用

请检查视图的autoResizing属性

最简单的方法是您可以为纵向和横向模式创建2个不同的视图,因此无论何时模式更改,您都可以轻松更改视图。这样的:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        self.view = self.portraitView;
    else 
        self.view = self.landscapeView;
    [tblScore reloadData];
    return YES;
}

您使用AutoResizing,无需为帧做编码。http://developer.apple.com/library/ios/文档/windowsviews/概念/viewpg_iphoneos/CreatingViews CreatingViews.html

最新更新