简体   繁体   English

将子视图添加到模糊视图

[英]Adding subview to a blurred view

        if !UIAccessibilityIsReduceTransparencyEnabled() {
            self.blurOutletTop.backgroundColor = UIColor.clear

            let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)

            let blurEffectView = UIVisualEffectView(effect: blurEffect)
            blurEffectView.frame = self.blurOutletTop.bounds
            blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

            self.view.addSubview(blurEffectView)

            //self.logoOutlet.addSubview(blurEffectView)

        }
        else {
            self.view.backgroundColor = UIColor.black
        }
    }

I've got a navigation bar sized UIView up near the top of my screen.我在屏幕顶部附近有一个导航栏大小的 UIView。 The above code adds a blur effect to it.上面的代码为其添加了模糊效果。 What I'm trying to do is add my navigation buttons ontop of the view so they're not blurred, but doing this just through the storyboard results in the buttons also being blurred.我想要做的是将我的导航按钮添加到视图的顶部,这样它们就不会模糊,但是仅通过故事板执行此操作会导致按钮也变得模糊。

I read that adding the button's outlets as subviews to the blurEffectView would place the buttons on top, but when I uncomment out我读到将按钮的出口作为子视图添加到 blurEffectView 会将按钮放在顶部,但是当我取消注释时

self.logoOutlet.addSubview(blurEffectView)

the blurOutletTop's position is relocated, the background of the button is blurred but the label text is not, and the button itself is relocated. blurOutletTop 的位置被重新定位,按钮的背景被模糊但标签文本没有,并且按钮本身被重新定位。 I've tried adding constraints and it hasn't helped either.我试过添加约束,但也没有帮助。

What's the easiest way to get my buttons/ images overtop of my blurTopOutlet?让我的按钮/图像覆盖在我的 blurTopOutlet 上的最简单方法是什么?

I've subclassed UIVisualEffectView in code, and then added all kinds of controls as subviews to it.我在代码中对 UIVisualEffectView 进行了子类化,然后将各种控件作为子视图添加到其中。 Very nice effect.很不错的效果。 Not sure if it can be done 100% through IB though due to view hierarchy expectations.由于视图层次结构的期望,不确定是否可以通过 IB 100% 完成。

  • (1) Create the "main" view in IB and add constraints. (1) 在IB中创建“主”视图并添加约束。 The alternative here is what I did, subclass UIVisualEffectView.这里的替代方案是我所做的,子类 UIVisualEffectView。
  • (2) Create the "main" view subviews in code, add them to the "main" view, and add constraints in code. (2) 在代码中创建“主”视图子视图,将它们添加到“主”视图中,并在代码中添加约束。
  • (3) Since you want certain actions associated to some of the subviews (the UIButtons), also be sure to code addTarget(target:for:) calls for them. (3) 由于您希望某些操作与某些子视图(UIButtons)相关联,还请确保为它们编写 addTarget(target:for:) 调用代码。

ADDED EDIT: You may be able to do most of this through IB.添加编辑:您可以通过 IB 完成大部分工作。 Set everything up, and since the UIButtons are subviews to your blurred view, try using bringSubview(ToFront:).设置所有内容,并且由于 UIButtons 是模糊视图的子视图,请尝试使用 becomeSubview(ToFront:)。 If that works, it's probably much easier!如果这有效,它可能会容易得多! (I'm old-school and like coding things more.) (我是老派,更喜欢编码。)

将您不想模糊的按钮和其他视图添加到blurEffectView.contentView

blurEffectView.contentView.addSubview(someButton)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM