将多个UIButtons添加到UIView

时间:2009-06-29 14:50:18

标签: iphone cocoa-touch uiview uibutton

我已经以编程方式向UIView添加了一些按钮(通过addSubview)。但是,它们显示为叠加层(因此我总是只看到最后一个按钮)。如何在现有按钮下添加新按钮?

此致

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分:3)

设置UIView的帧原点以在您希望的位置布置UIButtons:

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

答案 2 :(得分: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];

答案 3 :(得分:0)

感谢您的回答。

我使用此代码进行(水平)对齐:

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

它在技术上有效,但仍然覆盖了按钮。我必须找到一种方法,如何不覆盖它们......

相关问题