如何以编程方式在UIScrollView上添加对象/视图?

时间:2012-02-27 22:32:58

标签: ios uiscrollview

我一直在使用Interface Builder和storyboard来制作我的应用程序,但是对于这个签名捕获API,一切都是在代码中完成的。

我正在尝试将它实现到我的应用程序中,但我无法弄清楚如何在我的scrollview上添加UIView和Buttons。

我让它出现在我的视图控制器中,但它根本没有连接到scrollview;它出现在最上面。

这是我的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGFloat padding = self.view.frame.size.width/15;

    UIView *autoGraphView = [[[UIView alloc] initWithFrame:CGRectMake(padding, 300, 280, 160)] autorelease];
    autoGraphView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    autoGraphView.layer.borderWidth = 3;
    autoGraphView.layer.cornerRadius = 10;
    [autoGraphView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:autoGraphView];

    self.autoGraph = [T1Autograph autographWithView:autoGraphView delegate:self];
    [autoGraph setLicenseCode:@"4fabb271f7d93f07346bd02cec7a1ebe10ab7bec"];
    [autoGraph setShowDate:YES];
    [autoGraph setShowHash:YES];

    UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    // init withFrame:CGRectMake (50, 300, 200, 60)];
    [clearButton setFrame:CGRectMake(padding, 480, 130, 30)];
    [clearButton setTitle:@"Clear" forState:UIControlStateNormal];
    [clearButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [clearButton addTarget:self action:@selector(clearButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:clearButton];

    UIButton *autoDone = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    // initWithFrame:CGRectMake (50, 300, 200, 60)];
    [autoDone setFrame:CGRectMake(150 + padding, 480, 130, 30)];
    [autoDone setTitle:@"Done" forState:UIControlStateNormal];
    [autoDone setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [autoDone addTarget:self action:@selector(doneButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:autoDone];
}

1 个答案:

答案 0 :(得分:1)

您需要将子视图添加到scrollview,如果viewcontroller的根视图不是scrollview,您需要以某种方式访问​​它,通常是通过IBOutlet,并以这种方式添加它们

相关问题