2009-06-29 69 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]]; 
    } 

它的工作原理技术,但仍然覆盖的按钮。我必须找到一种方法,怎么不覆盖他们...