2016-06-22 24 views
0

我有以下代碼:中心子視圖中ParentView在Objective-C

UIView *parentView = [[UIView alloc] init]; 
parentView.frame = CGRectMake(0, 0, 100, 100); 

parentView.center = self.view.center; 

parentView.backgroundColor = [UIColor greenColor]; 

UIView *subView = [[UIView alloc] init]; 

subView.backgroundColor = [UIColor redColor]; 

subView.frame = CGRectMake(0, 0, 50, 50); 

subView.center = parentView.center; 

[parentView addSubview:subView]; 

[self.view addSubview:parentView]; 

將會產生以下結果:

enter image description here

爲什麼是綠色的觀點並不爲中心的紅色視圖因爲他們有相同的中心?

+0

嘗試打印查看和檢查的框架。設置中心後。 –

+0

他們共享相同的確切中心。我印刷的價值觀,他們是一樣的。 –

+0

這是你的問題。現在你明白了什麼是問題。 :) –

回答

0

要設置綠色視圖作爲中心的是上海華盈,但你設置紅色視圖是在它的中心是上海華盈的上海華...

修復具有最小量的方式代碼更改將改變紅色視圖的超級視圖。

[self.view addSubview:parentView]; 
[self.view addSubview:subView]; 

編輯:解釋一點點進一步發生了什麼事情。說當你將你的綠色視圖的中心設置爲(500,500)時。這是它的超視圖,所以它被設置在屏幕中間。然後,將紅色視圖的中心設置爲與綠色視圖中心相同的值(500,500)。但是它的父親是紅色的視圖,所以它從綠色視圖的原點被放置(500,500)。它被放置在靠近屏幕的右下方。

+0

但在我的情況下,greenview,redview他們共享與self.view根視圖相同的中心。 –

+0

我想我明白了。當redView添加到greenView時,redView將使用greenView提供的座標/框架/邊界。 –

+0

@johndoe編​​輯進一步解釋一下。 – Putz1103

1

center屬性蘋果UIView文檔狀態:

該中心它的父的座標系中指定並且在點處測量。設置此屬性會相應地更改框架屬性的值。

這意味着parentView中心將相對於其superview(屏幕截圖的白色背景視圖)。

爲了得到你需要做的是這樣期望的結果:

subview.center = CGPointMake(CGRectGetMidX(parentView.bounds), 
          CGRectGetMidY(parentView.bounds)); 

但你確實應該使用自動佈局對於這種類型的東西。

0

你做這樣

UIView *parentView = [[UIView alloc] init]; 
parentView.frame = CGRectMake(0, 0, 100, 100); 

parentView.center = self.view.center; 

parentView.backgroundColor = [UIColor greenColor]; 


UIView *subView = [[UIView alloc] init]; 

subView.backgroundColor = [UIColor redColor]; 

CGFloat SubviewX = (parentView.frame.size.width - 50)/2; 
CGFloat SubviewY = (parentView.frame.size.height - 50)/2; 

subView.frame = CGRectMake(SubviewX, SubviewY, 50, 50); 


[parentView addSubview:subView]; 
[self.view addSubview:parentView]; 
0

的最好辦法中心的視圖,即使視圖改變視圖框保持它的方式是使用約束:

UIView *parentView = [[UIView alloc] init]; 
parentView.translatesAutoresizingMaskIntoConstraints = NO; 

// You might want to add constraints for the view's size 
parentView.frame = CGRectMake(0, 0, 100, 100); 

parentView.backgroundColor = [UIColor greenColor]; 

[self.view addSubview:parentView]; 

NSContraint *xContraint = [NSLayoutConstraint constraintWithItem:parentView 
                 attribute:NSLayoutAttributeCenterX 
                 relatedBy:NSLayoutRelationEqual 
                  toItem:self.view 
                 attribute:NSLayoutAttributeCenterX 
                 multiplier:1.0 
                 constant:0.0]; 

[self.view addConstraint:xContraint]; 

NSContraint *yContraint = [NSLayoutConstraint constraintWithItem:parentView 
                 attribute:NSLayoutAttributeCenterY 
                 relatedBy:NSLayoutRelationEqual 
                  toItem:self.view 
                 attribute:NSLayoutAttributeCenterY 
                 multiplier:1.0 
                 constant:0.0]; 

[self.view addConstraint:yContraint]; 

UIView *subView = [[UIView alloc] init]; 
subView.translatesAutoresizingMaskIntoConstraints = NO; 

// You might want to add constraints for the view's size 
subView.frame = CGRectMake(0, 0, 50, 50); 

subView.backgroundColor = [UIColor redColor]; 

[parentView addSubview:subView]; 

NSContraint *xSubContraint = [NSLayoutConstraint constraintWithItem:subView 
                  attribute:NSLayoutAttributeCenterX 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:parentView 
                  attribute:NSLayoutAttributeCenterX 
                 multiplier:1.0 
                  constant:0.0]; 

[parentView addConstraint:xSubContraint]; 

NSContraint *ySubContraint = [NSLayoutConstraint constraintWithItem:subView 
                  attribute:NSLayoutAttributeCenterY 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:parentView 
                  attribute:NSLayoutAttributeCenterY 
                 multiplier:1.0 
                  constant:0.0]; 

[parentView addConstraint:ySubContraint]; 
+0

你的代碼在運行時會有一些衝突 – marosoaie

+0

@marosoaie除了兩個視圖的幀大小之外,爲什麼這個代碼有衝突? – Fantini

相關問題