【问题标题】:Scroll view page control image is not fitting exactly滚动视图页面控件图像不完全适合
【发布时间】:2016-08-14 02:21:15
【问题描述】:

我在上面有一个视图,我正在滚动视图 m 将图像添加到滚动视图作为子视图,它不完全适合宽度和高度

关注这个https://www.cocoacontrols.com/controls/tapagecontrol 通过添加自动布局 m 得到问题 bcz 这不是自动布局

- (void)setupScrollViewImages
{
    for (UIScrollView *scrollView in self.scrollViews) {
        [self.imagesData enumerateObjectsUsingBlock:^(NSString *imageName, NSUInteger idx, BOOL *stop) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(scrollView.frame) * idx, 0, CGRectGetWidth(scrollView.frame), CGRectGetHeight(scrollView.frame))];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.image = [UIImage imageNamed:imageName];
            [scrollView addSubview:imageView];
        }];
    }
}

【问题讨论】:

  • 为什么会有多个滚动视图?
  • 我只有一个滚动视图
  • 那你为什么要遍历self.scrollViews?它是一个只包含一个滚动视图的数组吗?
  • 试试这个代码 UIImage *image = [UIImage imageNamed:@"banner.png"]; self.imageView = [[UIImageView alloc] initWithImage:image] self.imageView.frame = CGRectMake (0,0,self.scrollView.frame.size.width,self.scrollView.frame.size.height); self.imageView.contentMode = UIViewContentModeScaleToFill; [self.scrollView addSubview:self.imageView]; self.scrollView.contentSize = self.imageView.frame.size; self.scrollView.bounces = NO;
  • 你测试我使用三个滚动视图我将编辑我的问题我只有一个滚动视图

标签: ios objective-c uiscrollview uipagecontrol


【解决方案1】:

在添加到scrollview 之前,您可以强制imageview 具有相同的大小。

CGSize size = scrollView.frame.size;
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, size.width, size.height)];

【讨论】:

  • 如果我从右侧给出自动布局前沿,从左侧给出正确的结果,则得到相同的结果
  • 我添加了链接你可以查看
【解决方案2】:

尝试如下,

你的视图层次应该是这样的:

ScrollView - UIView - YourImageview

不要直接在scrollview上添加imageview,添加一个view。

第二件事设置你的imageview的内容模式UIViewContentModeScaletoFill

希望这会有所帮助:)

【讨论】:

  • 你可以检查 qns 中的链接,因为当我使用自动布局时它会出现问题
  • 能够理解您想说的话。您问题中的链接是第三方库。所以参观它没有意义。你到底需要什么?
  • 如果您检查该第 3 方中的图像滚动是端到端图像,但如果您使用尺寸类并且自动布局图像宽度正在端到端扩展
  • 如果你想实施并检查它
  • 你到底想要什么?
【解决方案3】:

我认为下面一行可能可以解决您的问题

imgV.clipsToBounds=YES;

我正在使用下面的代码,它工作得很好

for (int i=0; i<count; i++) {
    // create imageView
    AsyncImageView *imgV = [[AsyncImageView alloc] initWithFrame:CGRectMake(i*topScrollView.frame.size.width, 0, topScrollView.frame.size.width, topScrollView.frame.size.height)];
    // set scale to fill
    imgV.contentMode=UIViewContentModeScaleAspectFill;
    imgV.clipsToBounds=YES;
    [[Model sharedInstance] setImageWithUrl:[imgArray objectAtIndex:i] forAsyncImageView:imgV];
    // apply tag to access in future
    imgV.tag=i+100;
    // add to scrollView
    [topScrollView addSubview:imgV];
}

【讨论】:

  • 当我滚动时,我的图像被宽度和小高度扩展没有效果
  • 我添加了链接你可以查看
猜你喜欢
  • 1970-01-01
  • 2012-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
相关资源
最近更新 更多