2009-06-29 83 views
1

我已經通過編程向UIView(通過addSubview)添加了一些按鈕。但是,它們顯示爲覆蓋圖(所以我總是隻能看到最後一個按鈕)。如何在現有按鈕下添加新按鈕?將多個UIButtons添加到UIView

問候

+0

你能通過陳述哪個軸你指的是當你說「低於」澄清的問題? – teabot 2009-06-29 15:34:16

+0

是的,我的意思是Y軸。 – Stefan 2009-06-29 16:14:43

回答

4

可以抵消這樣

int newX = previousButton.frame.origin.x + previousButton.frame.size.width ; 
int newY = previousButton.frame.origin.y ; 

,要麼設置新的按鈕框,當您創建它的按鈕:

[[UIButton alloc] initWithFrame:CGRectMake(newX,newY,100,100)]; 

或設置幀以後

newButton.frame = CGRectMake(newX,newY,100,100); 
3

設置了UIView的框架產地佈局在您希望的位置UIButtons:

CGRect buttonFrame = button.frame; 
buttonFrame.origin = CGPointMake(100.0f, 100.0f); 
button.frame = buttonFrame; 
view.addSubview(button); 
+1

你需要添加「button.frame = buttonFrame;」在2號或3號線之後完成這項工作。否則,你只是修改框架矩形的副本。 – 2009-06-29 15:00:59

+0

@eJames - 良好的電話 – teabot 2009-06-29 15:12:02

2

您可以使用insertSubview:atIndex方法或insertSubview:您的觀點belowSubview。

UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,100,100)]; 

[myView insertSubview:myButton belowSubview:previousButton]; 

OR

[myView insertSubview:myButton atIndex:0]; 
+0

我不確定Stefan是指Y或Z軸 - 但我們之間已經涵蓋了兩者。 – teabot 2009-06-29 15:13:07

0

謝謝您的回答傢伙。

我做的(水平)與此代碼一致:

if([myContainer.subviews lastObject] == nil){ 
     NSLog(@"NIL"); 
     [myContainer insertSubview:roundedButton atIndex:0]; 
    }else{ 
     [myContainer insertSubview:roundedButton belowSubview:[tagsContainer.subviews lastObject]]; 
    } 

它的工作原理技術,但仍然覆蓋的按鈕。我必須找到一種方法,怎麼不覆蓋他們...