简体   繁体   English

当两个子视图具有相同的父视图时,根据其他子视图进行居中

[英]center subview according to other subview when both have the same superview

If I have contentView and 2 subviews. 如果我有contentView和2个子视图。 I want to center subviewA according to subviewB 我想根据subviewB居中subviewA

subviewA and subviewB have the same superview. subviewA和subviewB具有相同的超级视图。

this is what I'm doing now to center subviewA(activity indicator) above uitableview. 这就是我现在要做的,是将uitableview上方的subviewA(活动指示器)居中。

self.subViewA.frame = CGRectMake(CGRectGetMidX(self.tableView.frame)-self.subViewA.bounds.size.width/2,
                                                                 CGRectGetMidY(self.tableView.frame)-self.subViewA.bounds.size.height/2,
                                                                 self.subViewA.bounds.size.width,
                                                                 self.subViewA.bounds.size.height);

Is there cleaner/short way ? 有没有更清洁的方法? any method that exist and I'm missing :) ? 任何存在的方法,我想念:)吗?

I thought maybe there is somthing like : subviewA.frame = centerInOtherFrame(self.subviewB.frame) 我认为也许有这样的事情:subviewA.frame = centerInOtherFrame(self.subviewB.frame)

您的方法可以简化为

self.subViewA.center = self.tableView.center;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM