最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Swift TipKit MaxDisplayCount doesn't work in UIHostingController on iOS - Stack Overflow

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论