如何以编程方式在ios中的scrollview中添加UIView?

问题描述:

我用下面的代码如何以编程方式在ios中的scrollview中添加UIView?

CGRect scrollserviceViewFrame1 = CGRectMake(60,610, 650, 150); 
    //CGRect scrollViewFrame =[[UIScreen mainScreen] applicationFrame]; 
    //UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame]; 
    UIScrollView *servicescrollView1 = [[UIScrollView alloc] initWithFrame:scrollserviceViewFrame1]; 
    [self.view addSubview:servicescrollView1]; 
    //scrollView.backgroundColor=[UIColor greenColor]; 
    [servicescrollView1 setBackgroundColor:[UIColor redColor]]; 
    [servicescrollView1 setCanCancelContentTouches:NO]; 
    //scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack; 
    servicescrollView1.clipsToBounds = YES;  // default is NO, we want to restrict drawing within our scrollview 
    servicescrollView1.scrollEnabled = YES; 
    servicescrollView1.autoresizesSubviews=YES; 
    [servicescrollView1 setContentSize: CGSizeMake(1050, 100)]; 
    //scrollView.delegate = self; 
    servicescrollView1.pagingEnabled = YES; 
    servicescrollView1.showsHorizontalScrollIndicator = NO; 
    [servicescrollView1 scrollRectToVisible:CGRectMake(servicescrollView1.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES]; 

    NSLog(@" scroll view width :%i ",servicescrollView1.frame.size.width); 
    NSLog(@"scroll view height :%i ",self.view.frame.size.height); 
    NSLog(@"self view width :%i",self.view.frame.size.width); 


    CGRect CGRectZero = CGRectMake(60,710,200, 100);  
    UIView* myserviceView1 = [[UIView alloc] initWithFrame: CGRectZero]; 
    myserviceView1.backgroundColor=[UIColor blackColor]; 
    [servicescrollView1 addSubview:myserviceView1]; 
    [myserviceView1 release]; 


    [servicescrollView1 release]; 

滚动视图中创建,butcannot添加到UIView的滚动型...

任何建议解决这个查询???

在此先感谢

+0

请妥善格式化您的问题。你可以阅读[FAQ](http://*.com/faq)以了解什么是一个好问题。 – Jasarien 2012-01-31 09:09:31

不远的地方创建myserviceView1,你(尝试)代码的底部重置一个常数(CGRectZero)。

然后当你创建你的myserviceView1 UIView时,你给它的帧大小为{0, 0, 0, 0}

+0

...谢谢....我改变了CGRect CGRectZero1 = CGRectMake(60,710,200,100)以下的底部代码; \t \t UIView * myserviceView1 = [[UIView alloc] initWithFrame:CGRectZero1]; \t myserviceView1.backgroundColor = [UIColor blueColor]; \t [servicescrollView1 addSubview:myserviceView1]; \t [myserviceView1 release];但问题没有解决......还有什么建议?在此先感谢 – 2012-01-31 09:23:34

+1

编辑您的原始问题以显示此更新的代码。然后将'CGRectZero1'中的*变量*变量名变成更明智的东西,比如'myServiceViewFrame'。 – 2012-01-31 09:29:22