在视图控制器/页面视图控制器中使图像视图全屏显示(swift)

Posted

技术标签:

【中文标题】在视图控制器/页面视图控制器中使图像视图全屏显示(swift)【英文标题】:make image view full screen in view controller/page view controller (swift) 【发布时间】:2015-06-17 15:40:39 【问题描述】:

我正在使用 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.

主视图控制器代码:

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.

应用代理:

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

【问题讨论】:

看看这个***.com/questions/21045630/… 这个对我很有帮助UIPageViewController UIImage not showing full screen 【参考方案1】:

UIPageViewController 有一个视图,它的页面将出现在其中。这些页面不能大于该视图 - 换句话说,它们在页面视图控制器内部

您的 UIPageViewController 的视图比屏幕小 - 它在底部为页面控件和按钮留出了空间,在顶部为导航栏留出了空间。是你自己造成的:

self.pageViewController.view.frame = 
    CGRectMake(0, 30, self.view.frame.width, self.view.frame.size.height - 60)

您允许顶部有 30 分的差距,底部有 30 分的差距。因此,它显示的图像永远不会比这更大。它们永远不可能是“全屏”。

【讨论】:

不能这样做吗? s-media-cache-ak0.pinimg.com/736x/a3/84/cd/… 显然可以。但这不是你问的。你问为什么不这样做。我告诉你为什么。 - 在我的回答中添加了更多信息,向您展示您自己如何使图像视图小于屏幕。 另外,虽然我只是猜测,但我会说您显示的 Telegram 界面不是 UIPageViewController - 它可能只是一个分页水平 UIScrollView。只有 6 个“页面”只是图像,无论如何我都会这样做。 谢谢!没听懂。固定底部间隙。将值更改为 0,0,width,height。由于某种原因,顶部差距仍在显示 好的,但重点是,我相信我已经回答了这个问题。您的问题不是“我如何实现 Telegram 界面”?您的问题是“为什么 my 界面中的图像不占据整个屏幕”?我已经解释过了。【参考方案2】:

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!)
    

【讨论】:

以上是关于在视图控制器/页面视图控制器中使图像视图全屏显示(swift)的主要内容,如果未能解决你的问题,请参考以下文章

iOS - 在呈现另一个视图控制器时堆栈全屏视图控制器

在不同的视图控制器中打开 webkit 视图

如何在 tableview 中为图像设置动画以同时扩展和打开另一个视图控制器?

在 Swift 中使 NavigationBar 子视图可点击

如何在 Swift iOS 中使 web 视图不可点击

无法显示子视图全屏尺寸和横向