请教下大佬们, swiftUI 菜单栏应用 onHover 事件触发问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
storyxc
V2EX    iDev

请教下大佬们, swiftUI 菜单栏应用 onHover 事件触发问题

  •  
  •   storyxc 2022-07-10 10:45:05 +08:00 8417 次点击
    这是一个创建于 1263 天前的主题,其中的信息可能已经有所发展或是发生改变。

    刚学习 swift 和 swiftUI 不久,目前在试着用 swiftUI 做一个纯菜单栏的 todolist 工具。现在调界面的时候碰到个问题:点击图标弹出应用后,子视图上 TodoRowView 的 onHover 事件无法触发,必须要点击一下弹出的这块区域才可以,请教下这个问题的解决办法,不胜感激

    主视图结构大概是

    struct HomeView: View { var body: some View { VStack { CustomSegmentedControl() ScrollView { TodoRowView() TodoRowView() TodoRowView() } HStack { xxxx.. } } } } 

    问题如下:

    out.gif

    第 1 条附言    2022-07-10 14:28:47 +08:00

    找到一个解决办法,菜单栏popover的代码我是看Kavsoft的视频抄的的,这块是AppKit的代码写的,并没有完全理解。只需要调整下popover.contentViewController?.view.window?.makeKey()这行代码的位置即可,把这行代码从setUpMacMenu中,挪到menuButtonAction的else中,这样还可以解决另一个问题:点击弹出区域外的位置,菜单弹出区域不会自动关闭。

    代码:

    class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate { private var statusItem: NSStatusItem? private var popover = NSPopover() func applicationDidFinishLaunching(_ notification: Notification) { setUpMacMenu() } func setUpMacMenu() { popover.animates = true popover.behavior = .transient popover.cOntentViewController= NSViewController() popover.contentViewController?.view = NSHostingView(rootView: HomeView()) //popover.contentViewController?.view.window?.makeKey() statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) if let menuButton = statusItem?.button { menuButton.image = .init(systemSymbolName: "d.circle.fill", accessibilityDescription: nil) menuButton.action = #selector(menuButtonAction) } } @objc func menuButtonAction() { if popover.isShown { popover.performClose(nil) }else { if let menuButton = statusItem?.button { popover.show(relativeTo: menuButton.bounds, of: menuButton, preferredEdge: .minY) popover.contentViewController?.view.window?.makeKey() } } } } 
    6 条回复    2022-07-10 17:57:21 +08:00
    lizhiqing
        1
    lizhiqing  
       2022-07-10 11:04:54 +08:00
    撞头像了
    storyxc
        2
    storyxc  
    OP
       2022-07-10 11:10:38 +08:00
    @lizhiqing #1 我记得我之前也因为头像回过你的帖子
    andyJado
        3
    andyJado  
       2022-07-10 12:44:13 +08:00   1
    1. 把 ScrollVIew 宕掉 //, 然后看看好不好使.

    2.建议亲把 onHover 逻辑剥离出来放到 HomeView 里.
    shoujiaxin
        4
    shoujiaxin  
       2022-07-10 13:51:24 +08:00   1
    popover 弹出的时候调用一下 NSApp.activate(ignoringOtherApps: true)
    storyxc
        5
    storyxc  
    OP
       2022-07-10 14:34:56 +08:00
    @andyJado #3 感谢回复,我试过这么做但是没有效果


    @shoujiaxin #4 感谢大佬!把这个加进去确实能解决正文中的问题。但是后来又发现个点击 popover 以外的区域时 popover 不会自动关闭的问题,我找到 youtube 上说调整 popover.contentViewController?.view.window?.makeKey()这行代码位置的回复,尝试之后这两个问题都解决了。 无论如何还是非常感谢!
    justin2018
        6
    justin2018  
       2022-07-10 17:57:21 +08:00   2
    Learn SwiftUI - Kavsoft

    https://kavsoft.dev/

    他的课程不错 实战挺好
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3210 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 10:56 PVG 18:56 LAX 02:56 JFK 05:56
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86