xcode - ios图片轮播器 frame异常

查看:216
本文介绍了xcode - ios图片轮播器 frame异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

写的图片轮播器,带式图片的ImageView刚好和整个view有个整个view距离其父view的距离。

具体代码如下:

-(instancetype)initWithFrame:(CGRect)frame imageNames:(NSArray *)imageNames{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.imageArr = imageNames;
        self.isPortrait = NO;
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:frame];
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.delegate = self;
        self.scrollView.pagingEnabled = YES;
        [self addSubview:self.scrollView];
        
        for (int i = 0; i < 3; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.layer.borderWidth = 2;
            imageView.layer.borderColor = [UIColor yellowColor].CGColor;
            [self.scrollView addSubview:imageView];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickImageViewWithBlock:)];
            [self.scrollView addGestureRecognizer:tap];
            
        }
        
        //pageControl
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height - 37, frame.size.width, 37)];
        [self addSubview:self.pageControl];
        
    }
    return self;
}

-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    CGFloat w = self.frame.size.width;
    CGFloat h = self.frame.size.height;
    
    if (self.isPortrait) {
        
        [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIImageView *img = (UIImageView *)obj;
            img.frame = CGRectMake(0, idx * w, w, h);
        }];
        
        self.scrollView.contentSize = CGSizeMake(0, imageViewCount * h);
        
    }else{
        
        [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIImageView *img = (UIImageView *)obj;
            img.frame = CGRectMake(idx * w, 0, w, h);
        }];
        
        self.scrollView.contentSize = CGSizeMake(imageViewCount * w, 0);
    }
    
    self.pageControl.numberOfPages = self.imageArr.count;
    self.pageControl.currentPage = 0;
    
    [self updateImage];
    
}

感觉代码逻辑并没有问题,然后找不到原因,求大神告知

解决方案

if (self.isPortrait) {
        
        [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIImageView *img = (UIImageView *)obj;
            img.frame = CGRectMake(0, idx * w, w, h);
        }];
        
        self.scrollView.contentSize = CGSizeMake(0, imageViewCount * h);
        
    }else{
        
        [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIImageView *img = (UIImageView *)obj;
            img.frame = CGRectMake(idx * w, 0, w, h);
        }];
        
        self.scrollView.contentSize = CGSizeMake(imageViewCount * w, 0);
    }

img.frame = CGRectMake(0, idx * w, w, h);

w 改为 h

这篇关于xcode - ios图片轮播器 frame异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆