iosswiftwatchkitapple-watchwkinterfacegroup

Can I scroll WKInterfaceGroup programmatically?


I have a label with a long text and a button inside a WKInterfaceGroup. Can I scroll the group to top on the button's touch handler?


Solution

  • You can do this since WatchOS 4. The WKInterfaceController has a function for this.

    func scroll(to object: WKInterfaceObject, at scrollPosition: WKInterfaceScrollPosition, animated: Bool)
    

    Simply specify the object (this could for example be a WKInterfaceGroup) you want to scroll to, and specify the scrollPosition on the screen it should scroll to (in your case .top) and you're good. So in your WKInterfaceController class:

    scroll(to: objectToShowAtTheTopOfTheScreen, at: .top, animated: true)
    

    Read more on this in the Apple Developer documentation for WKInterfaceController

    And the possible values for scrollPosition