找到你要的答案

Q:iOS Table View XIB layout issue

Q:iOS的表视图XIB的布局问题

I have declared x3 UITableView's in Xcode XIB interface builder:

Then I ran current UIViewController in iPad Air iOS Simulator:

As you see, top cell of the first UITableView view differs to second one / third one.

Why is that so?

How to make these UITableView'look like the same?

我已经宣布X3 UITableView在Xcode xib界面生成器:

然后我跑在iPad Air iOS模拟器当前窗口:

如你所见,第1 UITableView视图上细胞不同的二/第三。

为什么会这样?

如何让这些uitableview'look一样吗?

answer1: 回答1:

ok, i got it. override the init method like the following:

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    return self;
}

you have to take care of the insets yourself since this automatic property can only handle the inset for the first subview in the hierarchy.

好的,我知道了。重写init方法如下:

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    return self;
}

你要好好照顾自己的插图这自动属性只能处理在第一层次视图插入。

answer2: 回答2:

for the inset problem create those properties:

@property (weak, nonatomic) IBOutlet UITableView *tableViewLeft;
@property (weak, nonatomic) IBOutlet UITableView *tableViewMiddle;
@property (weak, nonatomic) IBOutlet UITableView *tableViewRight;

and then try the following:

- (void)viewDidLayoutSubviews {
    UIEdgeInsets inset = UIEdgeInsetsMake(self.topLayoutGuide.length, 0.0, 0.0, 0.0);
    self.tableViewLeft.contentInset = inset;
    self.tableViewMiddle.contentInset = inset;
    self.tableViewRight.contentInset = inset;
}

对于嵌入问题创建这些属性:

@property (weak, nonatomic) IBOutlet UITableView *tableViewLeft;
@property (weak, nonatomic) IBOutlet UITableView *tableViewMiddle;
@property (weak, nonatomic) IBOutlet UITableView *tableViewRight;

然后尝试以下:

- (void)viewDidLayoutSubviews {
    UIEdgeInsets inset = UIEdgeInsetsMake(self.topLayoutGuide.length, 0.0, 0.0, 0.0);
    self.tableViewLeft.contentInset = inset;
    self.tableViewMiddle.contentInset = inset;
    self.tableViewRight.contentInset = inset;
}
ios  xcode  uitableview  interface-builder  xib