ScrollView,视图没有显示

时间:2013-10-05 14:24:55

标签: ios objective-c uiscrollview addsubview

滚动视图显示,没有任何可见的视图。视图移动正常,页面控件正常工作。当我更改滚动背景,一切正常。 “不同颜色”的观点没有显示出来。不知道什么是错的。谢谢你的帮助

-(void)viewDidLoad {    
[super viewDidLoad];

CGRect frame = CGRectMake(0, 215, WIDTH_OF_IPHONE, HEIGHT_OF_SCROLLER);

int numOfPhotos = 6;

scrollViewer = [[UIScrollView alloc] initWithFrame:frame];
scrollViewer.contentSize = CGSizeMake((WIDTH_OF_IPHONE*numOfPhotos), HEIGHT_OF_SCROLLER);
scrollViewer.pagingEnabled = YES;
//scrollViewer.backgroundColor = [UIColor lightGrayColor];
[scrollViewer setDelegate:self];

int i = 0;

while (i < numOfPhotos){

    CGRect frameOfView = CGRectMake((i*320), 215, 320, HEIGHT_OF_SCROLLER);
    UIView *photoFrame = [[UIView alloc] initWithFrame:frameOfView];

    if (i == 0){
        photoFrame.backgroundColor = [UIColor redColor];//colorWithPatternImage:image1];
        NSLog(@"colour was selected");
    }else if (i==1){
        photoFrame.backgroundColor = [UIColor blueColor];//colorWithPatternImage:image2];
    }else if (i==2){
        photoFrame.backgroundColor = [UIColor purpleColor];//colorWithPatternImage:image3];
    }else if (i==3){
        photoFrame.backgroundColor = [UIColor orangeColor];//colorWithPatternImage:image4];
    }else if (i==4){
        photoFrame.backgroundColor = [UIColor magentaColor];//colorWithPatternImage:image5];
    }else{
        photoFrame.backgroundColor = [UIColor greenColor];//colorWithPatternImage:image6];
    }

    [scrollViewer addSubview:photoFrame];
    //[photoFrame release];

    photoFrame = NULL;
    i++;
}
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, WIDTH_OF_IPHONE, 15)];
pageControl.center = CGPointMake(scrollViewer.center.x, (208+HEIGHT_OF_SCROLLER));

[pageControl setNumberOfPages:numOfPhotos];

[pageControl addTarget:self action:@selector(pageSwiped:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:scrollViewer];
[self.view addSubview:pageControl];

我的.H文件看起来像这样

@interface HomePage : UIViewController <UIActionSheetDelegate, UIScrollViewDelegate> {

   IBOutlet UIScrollView *scrollViewer;
   UIPageControl *pageControl;
   BOOL pageControlUsed;

}
@end

1 个答案:

答案 0 :(得分:0)

尝试将photoFrame的y原点设置为0,因为它会将它放在滚动视图的实际帧下。

如果不是您遇到的问题是您正在使photoFrame为NULL。如果您使用ARC,您不必担心,因为垃圾收集器会处理它。