admin管理员组

文章数量:1316661

If you configure simple Tip with MaxDisplayCount option, this option doesn't have any effect when showing TipView in SwiftUI view which is wrapped in UIHostingController. The Tip never invalidates and remains visible until user dismisses it manually.

Tip definition:

struct CustomTip: Tip {
    
    var title: Text {
        Text("Tip title")
    }
    
    var message: Text? {
        Text("Tip message")
    }
    
    var options: [Option] {
        MaxDisplayCount(1)
    }
}

AppDelegate:

import UIKit
import SwiftUI
import TipKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        try? Tips.configure()
        
        let vc = UIHostingController(rootView: CustomView())
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = vc
        window?.makeKeyAndVisible()
        
        return true
    }
}

SwiftUI view:

import SwiftUI
import TipKit

struct CustomView: View {
    var body: some View {
        VStack {
            TipView(CustomTip())
            Text("Hello, World!")
        }
    }
}

If you configure simple Tip with MaxDisplayCount option, this option doesn't have any effect when showing TipView in SwiftUI view which is wrapped in UIHostingController. The Tip never invalidates and remains visible until user dismisses it manually.

Tip definition:

struct CustomTip: Tip {
    
    var title: Text {
        Text("Tip title")
    }
    
    var message: Text? {
        Text("Tip message")
    }
    
    var options: [Option] {
        MaxDisplayCount(1)
    }
}

AppDelegate:

import UIKit
import SwiftUI
import TipKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        try? Tips.configure()
        
        let vc = UIHostingController(rootView: CustomView())
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = vc
        window?.makeKeyAndVisible()
        
        return true
    }
}

SwiftUI view:

import SwiftUI
import TipKit

struct CustomView: View {
    var body: some View {
        VStack {
            TipView(CustomTip())
            Text("Hello, World!")
        }
    }
}
Share Improve this question asked Jan 29 at 9:15 Lukáš MatuškaLukáš Matuška 11 bronze badge 2
  • Isn't this the purpose of a Tip? The user have to manually close it. – Carsten Commented Jan 29 at 9:45
  • Tips are heavily dependent on the SwiftUI environment, try moving the configure line into SwiftUI – lorem ipsum Commented Jan 29 at 11:49
Add a comment  | 

1 Answer 1

Reset to default 0

I filed a Feedback report and got an answer from Apple stating that this is known issue they are hoping to fix in a future update - claiming it is due to how SwiftUI APIs use scenePhase. Recommended solution for now is to use TipUIView instead of the TipView for displaying the tip.

本文标签: Swift TipKit MaxDisplayCount doesn39t work in UIHostingController on iOSStack Overflow