如何修复附加到“分组"的边距调整代码;UITableView? [英] how to fix the margin adjustment code attached for a 'Grouped" UITableView?

查看:59
本文介绍了如何修复附加到“分组"的边距调整代码;UITableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图达到外观&分组表视图的感觉,每节只有一个项目,但是几乎没有空白(给我圆角的视图,使用户能够选择他们想要的颜色).

Trying to achieve the look & feel of a Grouped TableView, with only one item per section, and but with hardly any margins (gives me rounded edged view with the ability for the user to be able to choose the color they want).

我可以使用它,但是当用户更改方向时,我不得不使用didRotateFromInterfaceOrientation方法(因为willRotateToInterfaceOrientation不起作用),但是效果是,您确实看到在tableView显示.

I have it working, HOWEVER when the user changes orientation I had to use the didRotateFromInterfaceOrientation method (as willRotateToInterfaceOrientation didn't work), BUT the effect is that you do see the margins change quickly in that fraction of a second after the tableView displays.

问题-有什么方法可以解决问题,使您看不到这种转变吗?

QUESTION - Any way to fix things so one doesn't see this transition?

- (void) removeMargins {
  CGFloat marginAdjustment = 7.0;
  CGRect f = CGRectMake(-marginAdjustment, 0, self.tableView.frame.size.width + (2 * marginAdjustment), self.tableView.frame.size.height);
  self.tableView.frame = f;
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  [self removeMargins];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
  [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  [self removeMargins];
}

推荐答案

我认为问题是在willRotateToInterfaceOrientation中,tableView的框架尚未调整大小,因此您的框架计算不正确.在didRotateFromInterfaceOrientation上,框架已更改.

I Think the problem is that in willRotateToInterfaceOrientation the frame of the tableView hasn't resized yet, so your frame calculations are incorrect. on didRotateFromInterfaceOrientation the frame has already changed.

我认为解决此问题的最简单方法是子类化UITableView并覆盖layoutSubviews.每次视图框架以可能需要更改其子视图的方式更改时,都会调用此方法.

I think the easiest way to solve this is to subclass UITableView and override layoutSubviews. This method is called every time the frame of the view changes in a way that may require it's subviews to change.

以下代码在没有动画故障的情况下为我工作:

The following code worked for me without an animation glitch:

@interface MyTableView : UITableView {
}

@end

@implementation MyTableView

-(void) layoutSubviews
{
    [super layoutSubviews];

    CGFloat marginAdjustment = 7.0;

    if (self.frame.origin.x != -marginAdjustment) // Setting the frame property without this check will call layoutSubviews again and cause a loop
    {
        CGRect f = CGRectMake(-marginAdjustment, 0, self.frame.size.width + (2 * marginAdjustment), self.frame.size.height);
        self.frame = f;
    }
}

@end

这篇关于如何修复附加到“分组"的边距调整代码;UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆