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

时间:2012-01-31 09:05:55

标签: objective-c ipad uiview uiscrollview

我使用了以下代码

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];

滚动视图已创建,但无法将UIVIew添加到scrollview ...

解决此查询的任何建议???

提前致谢

1 个答案:

答案 0 :(得分:0)

在您创建myserviceView1的代码底部附近,您(尝试) 重置 一个常量(CGRectZero)。

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