2012-01-31 118 views
0

我用下面的代碼如何以編程方式在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://stackoverflow.com/faq)以瞭解什麼是一個好問題。 – Jasarien 2012-01-31 09:09:31

回答

0

不遠的地方創建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

相關問題