最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中让ViewController的背景是半透明的

iOS crifan 4625浏览 0评论

之前用:

        let filterVC = FilterCustomerViewController()

        self.presentViewController(filterVC, animated: true, completion: nil)

        self.view.backgroundColor = UIColor(hexString: "#C0C0C0", alpha: 0.5

但是没生效:

右边不是透明的。

swift viewcontroller half transparent

ios – Presenting semi-transparent viewcontroller that works both in iOS7 and iOS8 – Stack Overflow

uiviewcontroller – How to present a semi-transparent (half-cut) viewcontroller in iOS? – Stack Overflow

ios – How do I make a semi-transparent view layer above a current view? – Stack Overflow

swift – How to make UIPopoverPresentationController semi transparent for iOS 9 – Stack Overflow

uiviewcontroller – iOS – Semi-transparent modal view controller – Stack Overflow

Transparent View Controllers and Dim Backgrounds — Swift + iOS Developer Training

用代码:

        self.view.backgroundColor = UIColor(hexString: "#C0C0C0")

        self.view.alpha = 0.5

结果:

整个页面,全部都是蒙蒙的,且没有透明

不是我要的效果

最后用代码:

       let filterVC = FilterCustomerViewController()

        filterVC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

        self.presentViewController(filterVC, animated: true, completion: nil)

class FilterCustomerViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor(hexString: "#C0C0C0", alpha: 0.5)

}

达到了效果:

-》即,左边正常显示内容,右边部分是透明的

-》即,在原有基础上:

调用显示ViewController的时候,加上:

filterVC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

然后被调用的VC中的背景色,使用带alpha的color:

self.view.backgroundColor = UIColor(hexString: "#C0C0C0", alpha: 0.5)

即可。

[后记]

后来改为:

//        filterVC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

        filterVC.modalPresentationStyle = UIModalPresentationStyle.Custom

使的,前面的view没有到顶显示的问题,解决了:

转载请注明:在路上 » [已解决]swift中让ViewController的背景是半透明的

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.185 seconds, using 22.12MB memory