1

我正在为我的个人演练使用BWWalkthrough库。它的实现非常简单,但我对 BWWalkthroughViewController 有一个错误:我不会使用情节提要来创建这个主控制器,所以我通过代码创建了我的自定义视图,然后在 BWWalkthroughViewController 的 viewDidLoad 中设置了视图和 pageControl。其余代码与 github 存储库中的示例相同。

也就是 BWWalkthroughViewController 的 viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()
    let welcomeview: WelcomeView = WelcomeView() // my custom view
    view = welcomeview
    self.pageControl = welcomeview.pageControl 
    ...
}

并且,因为它是第一个视图,所以 rootViewController 设置为 BWWalkthroughViewController 并修改了 viewDidLoad。所以我有一个函数可以在 AppDelegate.swift 中设置这个演练:

func showWelcomeView() -> BWWalkthroughViewController{
    let storyboard = UIStoryboard(name: "Welcome", bundle: nil)

    let walkthrough: BWWalkthroughViewController = BWWalkthroughViewController()
    let page_zero = storyboard.instantiateViewControllerWithIdentifier("walk_0") as! UIViewController
    let page_one = storyboard.instantiateViewControllerWithIdentifier("walk_1") as! UIViewController
    let page_two = storyboard.instantiateViewControllerWithIdentifier("walk_2") as! UIViewController
    let page_three = storyboard.instantiateViewControllerWithIdentifier("walk_3") as! UIViewController
    let page_four = storyboard.instantiateViewControllerWithIdentifier("walk_4") as! UIViewController

    walkthrough.delegate = self
    walkthrough.addViewController(page_zero)
    walkthrough.addViewController(page_one)
    walkthrough.addViewController(page_two)
    walkthrough.addViewController(page_three)
    walkthrough.addViewController(page_four)




    return walkthrough
}

我在这里使用该功能:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.


    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window?.rootViewController = showWelcomeView() // func usedhere
    return true
}

该应用程序运行没有错误。我可以看到我的自定义视图和所有 4 个子视图(walk_0 ... walk_4),我可以滑动它们但不能正确滑动:可滚动是免费的,并且不会在每一页上停止。

所以...我的自定义视图错过了一些步骤?请帮帮我!

这是我的 iPhone 的屏幕截图,滚动位于 2 个子视图之间:在此处输入图像描述

4

3 回答 3

1

我已经通过在 BWWalkthroughViewController 中使用滚动视图的属性修复了这个问题

override func viewDidLoad() {
    super.viewDidLoad()

   ..

    scrollview.pagingEnabled = true
}

工作正常,但不知道为什么。该滚动视图的默认值pagingeEnabled = true在 init 中设置:

required init(coder aDecoder: NSCoder) {
    // Setup the scrollview
    scrollview = UIScrollView()
    scrollview.showsHorizontalScrollIndicator = false
    scrollview.showsVerticalScrollIndicator = false
    scrollview.pagingEnabled = true  // Default of scrollview

    // Controllers as empty array
    controllers = Array()

    super.init(coder: aDecoder)
}
于 2015-06-19T15:26:56.733 回答
1

BWWalkthroughViewController在类中添加以下功能。

override func viewDidLayoutSubviews(){
    // Constraints
        let metricDict = ["w":view.bounds.size.width,"h":view.bounds.size.height]

        // - Generic cnst
        for vc in controllers {
            vc.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view(h)]", options:[], metrics: metricDict, views: ["view":vc.view]))
            vc.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view(w)]", options:[], metrics: metricDict, views: ["view":vc.view]))
            scrollview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]|", options:[], metrics: nil, views: ["view":vc.view,]))
        }
    }

addViewController函数中删除以下行

// Constraints
let metricDict = ["w":vc.view.bounds.size.width,"h":vc.view.bounds.size.height]
    // - Generic cnst
        vc.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view(h)]", options:[], metrics: metricDict, views: ["view":vc.view]))
        vc.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view(w)]", options:[], metrics: metricDict, views: ["view":vc.view]))
        scrollview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]|", options:[], metrics: nil, views: ["view":vc.view,]))
于 2015-11-23T10:42:58.497 回答
0

你连接了吗

@IBOutlet var pageControl:UIPageControl?

在 BWWalkthroughViewController 中与您的视图控制器中的页面控制器?

在此处输入图像描述

于 2015-05-17T21:35:34.777 回答