将alpha添加到parentvc.view

发布于 2025-01-28 09:27:24 字数 1162 浏览 3 评论 0原文

在按钮上轻按时,我正在尝试将alpha添加到背景视图中。到目前为止,添加了模糊,但alpha并没有那么多。

如何将alpha添加到背景中,以便当底部出现时,背景将变得更暗且禁用。

 let maxDimmedAlpha: CGFloat = 0.2
lazy var dimmedView: UIView = {
        let view = UIView()
        view.backgroundColor = .black
        view.alpha = maxDimmedAlpha
        return view
    }()



 @objc func shareBtnClick() {
    dimmedView.frame =  self.parentVC.view.bounds
    dimmedView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    self.parentVC.view.addSubview(dimmedView)
    if self.parentVC.navigationController != nil {
        if self.parentVC.navigationController?.viewControllers.count == 1 {
            showBottomSheet()
        } else {
            NotificationCenter.default.post(name: NSNotification.Name("ShowBottomSheet"), object: nil, userInfo: ["itemId": modalSheet(), "delegate": self])
        }
    } else {
        showBottomSheet()
    }
}

func showBottomSheet() {
    let modalSheet = MainBottomSheet()
    modalSheet.data = self.modalSheet()
    modalSheet.delegate = self
    modalSheet.modalPresentationStyle = .overCurrentContext
    self.parentVC.present(modalSheet, animated: true)
}

I am trying to add alpha to the background view when tapped on a button. So far achieved adding blur but alpha not so much.

How can I add alpha to the background so that when the bottom sheet appears background will be darker and disabled.

 let maxDimmedAlpha: CGFloat = 0.2
lazy var dimmedView: UIView = {
        let view = UIView()
        view.backgroundColor = .black
        view.alpha = maxDimmedAlpha
        return view
    }()



 @objc func shareBtnClick() {
    dimmedView.frame =  self.parentVC.view.bounds
    dimmedView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    self.parentVC.view.addSubview(dimmedView)
    if self.parentVC.navigationController != nil {
        if self.parentVC.navigationController?.viewControllers.count == 1 {
            showBottomSheet()
        } else {
            NotificationCenter.default.post(name: NSNotification.Name("ShowBottomSheet"), object: nil, userInfo: ["itemId": modalSheet(), "delegate": self])
        }
    } else {
        showBottomSheet()
    }
}

func showBottomSheet() {
    let modalSheet = MainBottomSheet()
    modalSheet.data = self.modalSheet()
    modalSheet.delegate = self
    modalSheet.modalPresentationStyle = .overCurrentContext
    self.parentVC.present(modalSheet, animated: true)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深缘浅 2025-02-04 09:27:24

我能够使用Xcode中的此代码产生昏暗的效果,我不确定为什么它在您的项目中不起作用,但是有一种简单的调试方法。

我建议使用DEBUG查看层次结构,这是我认为Xcode最好的工具之一。这使您可以分开用户界面的每一层。这样,您可以查看您的dimmedview实际上正在添加到父视图中,并且其frame正在与父视图的bunds匹配。

请记住,如果您的背景是黑暗的,则不会看到此dimmedview,因为其backgroundColor设置为uicolor.black

debug视图层次结构按钮

I was able to produce the dimmed effect using this code in XCode, I'm not sure why it won't work in your project but there is an easy way to debug this.

I suggest using Debug View Hierarchy, one of XCode's best tools in my opinion. This allows you to separate every single layer of the user interface. This way, you can see if your dimmedView is actually being added to the parent view and that its frame is matching the parent view's bounds.

Keep in mind if your background is dark, you won't see this dimmedView because its backgroundColor is set to UIColor.black.

Debug View Hierarchy button

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文