ios 以编程方式将 UIPageViewController Transition Style 设置为 Scroll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24577403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:14:06  来源:igfitidea点击:

Programmatically set UIPageViewController Transition Style to Scroll

iosswiftios8

提问by sameetandpotatoes

I am using Xcode 6 and implementing UIPageViewControllerfor my app. I followed appcoda's tutorial, and everything works except the page indicators. This is because I cannot set the transition style of UIPageViewControllerto scroll.

我正在使用 Xcode 6 并UIPageViewController为我的应用程序实施。我遵循了appcoda 的教程,除了页面指示器外,一切正常。这是因为我无法设置UIPageViewController滚动的过渡样式。

Graphically, when I click on the PageViewController, the tab shows View Controller instead of Page View Controller like appcoda(See its image below)

在图形上,当我单击 时PageViewController,该选项卡会显示视图控制器而不是页面视图控制器appcoda(见下图)

enter image description here

在此处输入图片说明

This is what mine looks like:

这是我的样子:

enter image description here

在此处输入图片说明

And yes, my custom class is set to: UIPageViewControlleras it is in the tutorial. enter image description here

是的,我的自定义类设置为:UIPageViewController就像在教程中一样。 在此处输入图片说明

Programmatically, I try to set the transition style with:

以编程方式,我尝试使用以下方法设置过渡样式:

    self.pageViewController.transitionStyle = UIPageViewControllerTransitionStyle.Scroll

but it fails to build, telling me that it Could not find member transition style. One last weird thing I would like to point out is that if I just write self.pageViewController.transitionStyle, it builds successfully, but it still uses the Page CurlTransition.

但它无法构建,告诉我它无法找到成员转换样式。我想指出的最后一件奇怪的事情是,如果我只编写self.pageViewController.transitionStyle,它会成功构建,但它仍然使用Page CurlTransition。

Could anyone help me? I am using Swift, Xcode 6, and developing on the iOS 8 SDK.

有人可以帮助我吗?我正在使用 Swift、Xcode 6,并在 iOS 8 SDK 上进行开发。

回答by Mick MacCallum

You're getting an error because transitionStyleis a readonly property. If you want to set the transition style of the page controller programmatically, it can only be done during initialization with the method:

您收到错误,因为transitionStyle是只读属性。如果要以编程方式设置页面控制器的过渡样式,则只能在初始化时使用以下方法完成:

init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation navigationOrientation: UIPageViewControllerNavigationOrientation, options options: [NSObject : AnyObject]!) { ... }

More info in the documentation.

文档中的更多信息。

回答by Marcin D

Since nobody has answered the question. The issue is that when you create a UIPageViewController inside a Container, the container is first created with a UIViewController, and that s the controller you see your options for. In order to fix it you have to delete UIViewController where you set your custom UIPageViewController. Then drag UIPageViewController from the controls picker and hook up the embed segue from the container to that controller, then you can set your custom pager controller as well as set transition to scroll.

由于没有人回答这个问题。问题是,当您在容器内创建 UIPageViewController 时,首先使用 UIViewController 创建容器,这就是您看到的控制器选项。为了修复它,您必须删除设置自定义 UIPageViewController 的 UIViewController。然后从控件选择器中拖动 UIPageViewController 并将容器中的 embed segue 连接到该控制器,然后您可以设置自定义寻呼机控制器以及将过渡设置为滚动。

回答by Cilvet

Swift 3.0

斯威夫特 3.0

required init?(coder aDecoder: NSCoder) {
    super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
}

回答by Divyesh Gondaliya

in ios 8 swift3

在 ios 8 swift3

override init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : Any]? = nil) {
        super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: options)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

回答by Sebastian

Xcode 7.1 & Swift 2

Xcode 7.1 和 Swift 2

All other solutions did not fully work for me at the latest Swift, especially since my class also inherits from UIPageViewControllerDataSource and UIPageViewControllerDelegate.

在最新的 Swift 中,所有其他解决方案对我来说并不完全适用,尤其是因为我的类也继承自 UIPageViewControllerDataSource 和 UIPageViewControllerDelegate。

To set the PageViewController transition to .Scroll just add these 2 methods to your class:

要将 PageViewController 过渡设置为 .Scroll 只需将这两个方法添加到您的类中:

override init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : AnyObject]?) {
    super.init(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: options)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

回答by PL175

Easiest way to do this programmatically in Swift 2.0 (without subclassing):

在 Swift 2.0 中以编程方式执行此操作的最简单方法(无子类化):

class PageViewController: UIPageViewController {

     override init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation navigationOrientation: UIPageViewControllerNavigationOrientation, options options: [NSObject : AnyObject]!) {
          super.init(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: options) 
     }

}

回答by Jorge Barboza

I know this a little bit late, but I just had the same problem and realized that it was originated because I was using a simple UIViewController, even when my custom view controller class inherits from an UIPageViewController, aparently the graphic menu of xcode doesn't recognize it.

我知道这有点晚了,但我只是遇到了同样的问题,并意识到这是因为我使用了一个简单的 UIViewController,即使我的自定义视图控制器类继承自 UIPageViewController,显然 xcode 的图形菜单也没有认出来。

The solution was to delete in story board the view controller and drag a PageViewController to replace it. This will have the graphic menu with the scroll option.

解决方案是在故事板中删除视图控制器并拖动一个 PageViewController 来替换它。这将具有带有滚动选项的图形菜单。

回答by Dardan

This is how i solve this problem in Swift 2.0

1. Created a class which is subclass of UIPageViewController

这就是我在 Swift 2.0 中解决这个问题的方法

1. 创建了一个类,它是 UIPageViewController 的子类

    import UIKit

    class OPageViewController: UIPageViewController {

        override init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : AnyObject]?) {

           // Here i changed the transition style: UIPageViewControllerTransitionStyle.Scroll           
            super.init(transitionStyle: UIPageViewControllerTransitionStyle.Scroll, navigationOrientation: UIPageViewControllerNavigationOrientation.Horizontal, options: options)
        }

        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

        override func viewDidLoad() {
            super.viewDidLoad()
        }


    }

2.Than i just used it :D

2.比我刚用过:D

pageController = OPageViewController()
pageController.dataSource = self
pageController.setViewControllers(NSArray(object: vc) as? [UIViewController], direction: .Forward, animated: true, completion: nil)
pageController.view.frame = CGRectMake(0, container.frame.origin.y, view.frame.size.width, container.frame.size.height);

回答by A. Welch

Swift 4 programmatically set the transition style of UIPageViewController

Swift 4 以编程方式设置 UIPageViewController 的过渡样式

This is straight from 'UIPageViewController.h' line 61-97

这是直接来自“UIPageViewController.h”第 61-97 行

Just put this before viewDidLoad() in your UIPageViewController() class

只需将它放在 UIPageViewController() 类中的 viewDidLoad() 之前

    override init(transitionStyle style: 
UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : Any]? = nil) {
            super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        }
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

回答by evevF

Swift 5 & 4: Just put this 2 functions before viewDidLoad() in your pageview VC

Swift 5 & 4:只需将这 2 个函数放在您的页面视图 VC 中的 viewDidLoad() 之前

override init(transitionStyle style: UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.NavigationOrientation, options: [UIPageViewController.OptionsKey : Any]? = nil) {
    super.init(transitionStyle: .pageCurl, navigationOrientation: .horizontal, options: nil)
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}