我正在使用UIPageViewController全屏显示图像。问题是图像没有全屏显示。相反,图像与页面控制视图点之间的底部和顶部之间存在间隙。我有一个UIViewController,作为子视图/子项添加到UIPageController,ViewController使用ImageView显示图像。我试图在swift / storyboard中做到这一点。
我在故事板中将顶部栏和底栏选项更改为“无”,但它不起作用。
图片预览:
ChildViewControllerCode:
class BoatContentViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
var pageIndex: Int!
var titleText: String!
var imageFile: String!
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.image = UIImage(named: self.imageFile)
self.titleLabel.text = self.titleText
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
mainViewControllerCode:
class BoatViewController: UIViewController, UIPageViewControllerDataSource {
@IBOutlet weak var loginButton: UIButton!
@IBOutlet weak var skipButton: UIButton!
var pageViewController: UIPageViewController!
var pageTitles: NSArray!
var pageImages: NSArray!
override func viewDidLoad() {
super.viewDidLoad()
loginButton.backgroundColor = UIColor.clearColor()
loginButton.layer.cornerRadius = 5
loginButton.layer.borderWidth = 1
loginButton.layer.borderColor = UIColor.purpleColor().CGColor
skipButton.backgroundColor = UIColor.whiteColor()
skipButton.layer.cornerRadius = 5
skipButton.layer.borderWidth = 1
skipButton.layer.borderColor = UIColor.whiteColor().CGColor
self.pageTitles = NSArray(objects: "", "", "", "", "")
self.pageImages = NSArray(objects: "onboarding1", "onboarding2", "onboarding3", "onboarding4", "onboarding5")
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("BoatPageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
var startVC = self.viewControllerAtIndex(0) as BoatContentViewController
var viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.size.height - 60)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
// Do any additional setup after loading the view.
}
func viewControllerAtIndex(index: Int) -> BoatContentViewController {
if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
return BoatContentViewController()
}
var vc: BoatContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("BoatContentViewController") as! BoatContentViewController
vc.imageFile = self.pageImages[index] as! String
vc.titleText = self.pageTitles[index]as! String
vc.pageIndex = index
return vc
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
var vc = viewController as! BoatContentViewController
var index = vc.pageIndex as Int
if (index == 0 || index == NSNotFound)
{
return nil
}
index--
return self.viewControllerAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
var vc = viewController as! BoatContentViewController
var index = vc.pageIndex as Int
if (index == NSNotFound) {
return nil
}
index++
if (index == self.pageTitles.count) {
return nil
}
return self.viewControllerAtIndex(index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return self.pageTitles.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
App Delegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var pageControl = UIPageControl.appearance()
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.blackColor()
pageControl.backgroundColor = UIColor.clearColor()
return true
}
答案 0 :(得分:2)
UIPageViewController有一个视图,其中将显示其页面。那些页面不能大于该视图 - 换句话说,它们是内部页面视图控制器。
您的UIPageViewController的视图小于屏幕 - 它允许页面控件和按钮的底部空间,以及导航栏的顶部。你自己导致这是真的:
self.pageViewController.view.frame =
CGRectMake(0, 30, self.view.frame.width, self.view.frame.size.height - 60)
您可以在顶部留出30个点,在底部留出30个点。因此,它显示的图像永远不会大于那个。它们永远不会是“全屏”。
答案 1 :(得分:0)
Swift 3 版本,当子类化UIPageViewController时:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let subViews: NSArray = view.subviews as NSArray
var scrollView: UIScrollView? = nil
var pageControl: UIPageControl? = nil
for view in subViews {
if view is UIScrollView {
scrollView = view as? UIScrollView
} else if view is UIPageControl {
pageControl = view as? UIPageControl
}
}
if (scrollView != nil && pageControl != nil) {
scrollView?.frame = view.bounds
view.bringSubview(toFront: pageControl!)
}
}