user3746428
user3746428

Reputation: 11175

Launch Watch App into middle view

Basically, my app is laid out in the page format and I would like it to launch into the middle of the three pages. There is no way of setting a previous page segue, so I have been trying to do it in code.

I have the main view set to the first view, and I have tried a variety of methods to segue to the middle view as soon as the app is launched.

Here is the two ways I tried:

    if segueCheck == true {
        self.pushControllerWithName("budget", context: self)
        self.presentControllerWithName("budget", context: self)
        segueCheck = false
    }

The first presents the view, but as a completely separate view, and the second replaces the first view with the middle view.

Does anyone know how I can launch into the middle view and allow the user to swipe left and right of it?

Thanks.

Upvotes: 8

Views: 2156

Answers (3)

Tap Forms
Tap Forms

Reputation: 911

The new way to do this in watchOS 4 and higher is:

WKInterfaceController.reloadRootPageControllers(withNames: 
["Controller1" "Controller2", "Controller3"], 
 contexts: [context1, context2, context3],
 orientation: WKPageOrientation.horizontal,
 pageIndex: 1)

Now you don't get the annoying animation when using becomeCurrentPage() when you want to start with the middle page.

Upvotes: 1

Harjot Singh
Harjot Singh

Reputation: 6927

Update Swift 3.0

class CenterPageViewController: WKInterfaceController {

override init (){
    super.init()
    super.becomeCurrentPage()
  }
}

This will works...!!!

Thanks

Upvotes: 1

Tomas Camin
Tomas Camin

Reputation: 10096

WKInterfaceController's becomeCurrentPage() should be what you're looking for.

Let's create a new class for the center view controller, CenterPageViewController, and change its initWithContext: method as follows

import WatchKit

class CenterPageViewController: WKInterfaceController {

    override init(context: AnyObject?) {
        super.init(context: context)

        super.becomeCurrentPage()        
    }
} 

Now let's set the Custom Class for the middle page in your storyboard to CenterPageViewController

enter image description here

and finally hit run.

You won't be able to get rid of the initial transition from the left page to the center page, but the app will finally begin on the middle page.

Upvotes: 8

Related Questions