2015-05-15 17 views
1

我使用BWWalkthrough圖書館作爲我的個人演練。它很容易實現,但我有一個BWWalkthroughViewController的錯誤:我不會使用storyboard來創建這個主控制器,所以我已經通過代碼創建了我的自定義視圖,然後在BWWalkthroughViewController的viewDidLoad中設置了視圖和pageControl。其餘代碼與github存儲庫中的示例相同。使用BWWalkthrough Library,無故事板,演練定製

即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:

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:

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),我可以將它們滑動但沒有正確顯示:滾動條是免費的,不會停留在每個頁面上。

所以......我錯過了我的自定義視圖的一些步驟?請幫幫我!

這個位於2子視圖之間從我的IPhone截圖與滾動:enter image description here

回答

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) 
} 
+0

兄你能幫我看看我的問題https://stackoverflow.com/questions/45004255/how-to-add-label-in-bwwalkthrough-view-controller-with-swift-3? –

0

你在BWWalkthroughViewController在您的視圖控制器的頁面控制器連接

@IBOutlet var pageControl:UIPageControl?

enter image description here

+0

我無法連接該照片直接,因爲我有我的自定義類...或者告訴我怎樣才能連接是^^ – Patonz

+0

導航到BWWalkthroughViewController和定位我在上圖中突出顯示的行。單擊並拖動左側裝訂線中的空白點到視圖控制器中的頁面控制視圖。 – rednivlat

+0

@rednivlat,兄弟你能幫我看看我的問題https://stackoverflow.com/questions/45004255/how-to-add-label-in-bwwalkthrough-view-controller-with-swift-3? –

1

附加以下功能BWWalkthroughViewController類。從addViewController功能

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,])) 
     } 
    } 

刪除以下行

// 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,]))