带有 Aspect Fill 图像模式的奇怪 UIPageViewController 滑动行为
Posted
技术标签:
【中文标题】带有 Aspect Fill 图像模式的奇怪 UIPageViewController 滑动行为【英文标题】:Weird UIPageViewController sliding behavior with Aspect Fill image mode 【发布时间】:2016-05-03 20:44:19 【问题描述】:我刚刚创建了一个带有页面内容视图控制器的UIPageViewController
,它由单个ImageView
和一个Button
组成。它几乎完美无缺,但是当我尝试仅在垂直视图中滑动视图时,它有一个主要问题,为我的ImageView
设置了纵横比填充模式(这基本上是为此设置的)。
您可以在此处看到此行为:https://youtu.be/2yl6UXbUGQg
我正在使用自动布局,Button 约束与 ImageView 对齐(全尺寸),ImageView 约束设置为 Superview...
代码:
1)UIPageViewController
:
class ViewController: UIPageViewController, UIPageViewControllerDataSource
var arrPageTitle: NSArray = NSArray()
var arrPagePhoto: NSArray = NSArray()
override func viewDidLoad()
super.viewDidLoad()
arrPageTitle = ["page 1", "page 2", "page 3"]
arrPagePhoto = ["slider-1.jpg", "slider-2.jpg", "slider-3.jpeg"]
self.dataSource = self
self.setViewControllers([getViewControllerAtIndex(0)] as [UIViewController],
direction: .Forward,
animated: false,
completion: nil)
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
let pageContent: PageContentViewController = viewController as! PageContentViewController
var index = pageContent.pageIndex
if ((index==0) || (index == NSNotFound))
return nil
index -= 1
return getViewControllerAtIndex(index)
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
let pageContent: PageContentViewController = viewController as! PageContentViewController
var index = pageContent.pageIndex
if index == NSNotFound
return nil
index += 1
if index == arrPageTitle.count
return nil
return getViewControllerAtIndex(index)
func getViewControllerAtIndex(index: NSInteger) -> PageContentViewController
let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController
pageContentViewController.strTitle = "\(arrPageTitle[index])"
pageContentViewController.strPhotoName = "\(arrPagePhoto[index])"
pageContentViewController.pageIndex = index
return pageContentViewController
2) 内容视图控制器,UIViewController
:
class PageContentViewController: UIViewController
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var labelTitle: UIButton!
var pageIndex: Int = 0
var strTitle: String!
var strPhotoName: String!
override func viewDidLoad()
super.viewDidLoad()
self.imageView.image = UIImage(named: self.strPhotoName)
self.labelTitle.setTitle(self.strTitle, forState: .Normal)
【问题讨论】:
【参考方案1】:您来自一个视图控制器的图像视图溢出到其他视图控制器,因为您需要在图像视图上将clipsToBounds
设置为true
。这将防止图像视图的内容超出图像视图的范围。
【讨论】:
【参考方案2】:检查Clip Subviews
以防止图像视图大小被拉伸。
【讨论】:
以上是关于带有 Aspect Fill 图像模式的奇怪 UIPageViewController 滑动行为的主要内容,如果未能解决你的问题,请参考以下文章
React-Camera:未定义不是对象(评估'CameraManager.Aspect.fill)
调整动画大小后UIButton不尊重Aspect Fill contentMode