如何使用两个UI按钮自定义UIview

时间:2012-06-18 12:23:07

标签: iphone ipad

如何使用两个UIview来保留UIbutton,其中用户可以将参数传递给自定义类

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    CanvasView *newCanvas = [[CanvasView alloc] initWithFrame:CGRectMake(0.0, 164.0, self.view.frame.size.width, self.view.frame.size.height-350)];
    self.canvas = newCanvas;
    [self.view addSubview:canvas];

}

我只能自定义UIView!如何将两个UIButton添加到View。

当我分配UIView自定义类时,它也需要显示UIButtons

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码

创建动态按钮
CanvasView *newCanvas = [[CanvasView alloc] initWithFrame:CGRectMake(0.0, 164.0, self.view.frame.size.width, self.view.frame.size.height-350)];
self.canvas = newCanvas;
[self.view addSubview:newCanvas];


UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
but.frame= CGRectMake(200, 15, 15, 15);
[but setTitle:@"Ok" forState:UIControlStateNormal];
[but addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[newCanvas addSubview:but];

您可以使用insertSubview:atIndex方法

 [newCanvas insertSubview:myButton atIndex:0];

希望它对你有所帮助。