Mert Köksal
Mert Köksal

Reputation: 901

Add alpha to parentVC.view

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)
}

Upvotes: 0

Views: 43

Answers (1)

Blu Nautilus
Blu Nautilus

Reputation: 1

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

Upvotes: 0

Related Questions