为什么[自我超级视图] .center不是指中心?

时间:2015-06-06 15:14:32

标签: cocoa-touch uiview

此代码:

m_networkActivityView = [[[NSBundle mainBundle] loadNibNamed:@"NetworkActivityView" owner:self options:nil] objectAtIndex:0];
[self addSubview:m_networkActivityView];
m_networkActivityView.center = self.center;
m_networkActivityView.hidden = true;

让我知道这一点,因为它以桌子为中心,而不是屏幕,因此偏离中心:

enter image description here

这段代码

m_networkActivityView = [[[NSBundle mainBundle] loadNibNamed:@"NetworkActivityView" owner:self options:nil] objectAtIndex:0];
[self addSubview:m_networkActivityView];
m_networkActivityView.center = [self superview].center;
m_networkActivityView.hidden = true;

告诉我这个:

enter image description here

我真的不明白。

1 个答案:

答案 0 :(得分:0)

我在评论中检查了superview,并且它是null,这就是它出现在角落里的原因。所以我这样做是为了解决它:

m_networkActivityView.center = 
    CGPointMake([UIScreen mainScreen].bounds.size.width / 2,
    [UIScreen mainScreen].bounds.size.height / 2);