iOS14 UIPageControl和scrollToItem

2020-11-26  本文已影响0人  梦里桃花舞倾城

BUG一:UIPageControl不显示

之前好端端的UIPageControl到了iOS14突然就不显示了表示很郁闷☹️

解决方案

1、按照系统规则适配pageControl size

size = CGSizeMake(_imagesArray.count * 10 * 1.5, 10);
if (@available(iOS 14.0, *)) {
    UIPageControl *pageControl = (UIPageControl *)_pageControl;
    size.width = [pageControl sizeForNumberOfPages:_imagesArray.count].width;
}
_pageControl.frame = xxxx;

2、添加约束适配(或者使用第三方添加约束)

size = CGSizeMake(_imagesArray.count * 10 * 1.5, 10);
self.pageControl.translatesAutoresizingMaskIntoConstraints = NO;
[[self.pageControl.bottomAnchor constraintEqualToAnchor:self.bottomAnchor
                                               constant:-_pageControlBottomOffset] setActive:YES];
[[self.pageControl.heightAnchor constraintEqualToConstant:size.height] setActive:YES];
if (_pageControlAliment == YXCycleScrollViewPageContolAlimentRight) {
  [[self.pageControl.rightAnchor constraintEqualToAnchor:self.rightAnchor
                                                          constant:-_pageControlRightOffset] setActive:YES];
} else{
   [[self.pageControl.centerXAnchor constraintEqualToAnchor:self.centerXAnchor] setActive:YES];
}

BUG二:collectionView scrollToItemAtIndexPath失效

这里失效的原因是对UICollectionView设置了分页属性pagingEnabled导致的,所以方案一我们可以关闭分页效果。

UICollectionViewLayoutAttributes *attributes = [_collectionView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0]];
_collectionView.contentOffset = CGPointMake(attributes.frame.origin.x - _itemSpacing, 0);
上一篇下一篇

猜你喜欢

热点阅读