React Native上的浮动按钮(FAB)在呈现模态,iOS动作表,选取器组件时不会保持在顶部

问题描述 投票:6回答:2

在高阶组件中使用绝对定位按钮将在正常使用情况下起作用,但是当渲染模态/动作表/拾取器等时,按钮不再保持在顶部.enter image description here

android ios react-native floating-action-button
2个回答
4
投票

免责声明:这样做几乎肯定会导致您的应用从应用商店中被拒绝,因此您应该确保它只显示在测试版和内部版本上。如果您需要Apple接受它,我建议通过React Native实现UIActionSheets和UIAlerts;有很多好的库可以模拟模态。

你需要在本机方面这样做。您可以将以下代码添加到AppDelegate:

var debugWindow: UIWindow?

@objc func pressButton(_ sender: UIButton) {
    print("Do debugging here")
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let screenSize = UIScreen.main.bounds

    let buttonController = UIViewController()
    let button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    button.setTitle("+", for: .normal)
    button.backgroundColor = UIColor.blue
    button.addTarget(self, action: #selector(pressButton(_:)), for: .touchUpInside)
    button.layer.cornerRadius = 25
    button.layer.masksToBounds = true
    buttonController.view = button

    debugWindow = UIWindow.init(frame: CGRect(x: screenSize.width - 100, y: screenSize.height - 100, width: 50, height: 50))
    debugWindow!.rootViewController = buttonController
    debugWindow!.windowLevel = UIWindow.Level.alert + 1000;
    debugWindow!.makeKeyAndVisible()

    return true
}

无论显示什么模态,这都会创建一个可以点击的按钮:iOS Simulator with floating debug button.


5
投票

不幸的是,这是不可能的。模态,操作表和警报在具有更高level的新窗口中呈现,因此它们将涵盖所有内容。您也无法插入包含FAB的更高级别的窗口,因为触摸事件不会降低到常规视图。有关此here的更多文档。

我还认为你不应该试图做到这一点。截图中的动作选择器应该绝对覆盖该动作按钮,如果您正在呈现模态,那么您可以随时尝试添加新的FAB。

© www.soinside.com 2019 - 2024. All rights reserved.