objective-c - 为什么图像不显示在 UIScrollView 中?

标签 objective-c ios

我正在使用这段代码来显示滚动图像,但我只得到一个白屏。我拖放 uiscrollview,创建了 iboutlet,合成了它,剩下的代码在这里

@synthesize scrollView1;

const CGFloat kScrollObjHeight  = 199.0;
const CGFloat kScrollObjWidth   = 280.0;
const NSUInteger kNumImages     = 5;

- (void)layoutScrollImages
{
    UIImageView *view = nil;
    NSArray *subviews = [scrollView1 subviews];

    // reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = 0;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
        {
            CGRect frame = view.frame;
            frame.origin = CGPointMake(curXLoc, 0);
            view.frame = frame;

            curXLoc += (kScrollObjWidth);
        }
    }

    // set the content size so it can be scrollable
    [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    scrollView1 = [[UIScrollView alloc]init];

    bgArray = [NSArray arrayWithObjects:@"Bg", @"Bg1.png", @"Bg2.png", @"Bg3.png", @"Bg4.png", @"Bg5.png", @"Bg6.png", @"Bg7.png", @"Bg8.png", nil];

    [scrollView1 setBackgroundColor:[UIColor blackColor]];
    [scrollView1 setCanCancelContentTouches:NO];
    scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView1.clipsToBounds = YES;        // default is NO, we want to restrict drawing within our scrollview
    scrollView1.scrollEnabled = YES;

    // pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
    // if you want free-flowing scroll, don't set this property.
    scrollView1.pagingEnabled = YES;

    // load all the images from our bundle and add them to the scroll view
    NSUInteger i;
    for (i = 0; i < [bgArray count]; i++)
    {
        NSString *imageName = [NSString stringWithFormat:@"%@", [bgArray objectAtIndex:i]];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
        CGRect rect = imageView.frame;
        rect.size.height = 199.0;
        rect.size.width = 280.0;
        imageView.frame = rect;
        imageView.tag = i;  // tag our images for later use when we place them in serial fashion
        [scrollView1 addSubview:imageView];
        [scrollView1 setShowsHorizontalScrollIndicator:NO];
        [scrollView1 setShowsVerticalScrollIndicator:NO];
    }

    [self layoutScrollImages];

}

最佳答案

我想如果你是从代码创建 UIScrollView

scrollView1 = [[UIScrollView alloc]init];

然后请在 View 中添加scrollView1 示例:-[self.view addSubview:scrollView1];

如果你从 nib 创建 UIScrollView 那么 不使用此代码

scrollView1 = [[UIScrollView alloc]init];

解决你的问题

关于objective-c - 为什么图像不显示在 UIScrollView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12001949/

相关文章:

ios - 尝试从第1节删除第2行,该行仅包含2行,然后进行更新

ios - 在运行 ios 13 的设备上,如果应用程序崩溃,则无法播放音频文件

ios - Cocoa pods 如果没有版本安装,它会安装旧包

ios - UICollectionViewLayout 'locked' 错误信息

ios - 图像未在内存中释放

html - NSAttributedString 的最后一行未在 UILabel 中呈现

ios - XCode 5.1 Beta 4无法识别iOS 7.1 Beta 4测试设备

ios - 一个没有年份的日期字符串到有年份的 NSDate

ios - 以编程方式更改多个按钮

ios - [NSKeyedUnarchiver initForReadingWithData :]: data is NULL