【发布时间】:2010-11-06 17:21:24
【问题描述】:
我以编程方式向 UIView(通过 addSubview)添加了一些按钮。但是,它们显示为叠加层(因此我总是只看到最后一个按钮)。如何在现有按钮下方添加新按钮?
问候
【问题讨论】:
-
您能否通过说明当您说“下方”时所指的轴来澄清问题?
-
是的,我指的是 y 轴。
标签: iphone cocoa-touch uiview uibutton
我以编程方式向 UIView(通过 addSubview)添加了一些按钮。但是,它们显示为叠加层(因此我总是只看到最后一个按钮)。如何在现有按钮下方添加新按钮?
问候
【问题讨论】:
标签: iphone cocoa-touch uiview uibutton
你可以像这样偏移按钮
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);
【讨论】:
设置 UIView 的框架原点以将 UIButtons 布局在您希望的位置:
CGRect buttonFrame = button.frame;
buttonFrame.origin = CGPointMake(100.0f, 100.0f);
button.frame = buttonFrame;
view.addSubview(button);
【讨论】:
您可以使用视图的 insertSubview:atIndex 方法或 insertSubview:belowSubview。
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,100,100)];
[myView insertSubview:myButton belowSubview:previousButton];
或
[myView insertSubview:myButton atIndex:0];
【讨论】:
谢谢你们的回答。
我做了(水平)与此代码对齐:
if([myContainer.subviews lastObject] == nil){
NSLog(@"NIL");
[myContainer insertSubview:roundedButton atIndex:0];
}else{
[myContainer insertSubview:roundedButton belowSubview:[tagsContainer.subviews lastObject]];
}
它在技术上有效,但仍然覆盖按钮。我必须找到一种方法,如何不覆盖它们......
【讨论】: