【问题标题】:UIViews in swift快速的 UIViews
【发布时间】:2014-10-10 14:01:34
【问题描述】:

全部,

我有一个图像,然后我将它显示在屏幕上,然后我转到一个函数来模糊图像。然后我运行另一个函数来添加一个框。但它没有显示模糊和框,这是因为 addSubView 和 insertSubView - 我想。基本上我不能将模糊和框都放在视图上。如果我取消注释 addBox 它不会显示模糊。谁能帮助我理解 addSubView 和 InsertSubView (数组)。

这是我的代码:

class ViewController: UIViewController {
    var Box : UIView?


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        let bananaImage : UIImage = UIImage(named: "edify-backgound.png")
        var imageV : UIImageView = UIImageView(image: bananaImage)
        imageV.frame = CGRectMake(0, 0, bananaImage.size.width, bananaImage.size.height)
        imageV.center = self.view.center
        self.view.addSubview(imageV)
        blur()
        //addBox(CGRectMake(200, 300, 30, 30))

    }

    func addBox(location: CGRect)
    {
        let newBox = UIView(frame: location)
        newBox.backgroundColor = UIColor.clearColor()
        self.view.insertSubview(newBox, atIndex: 1)
        Box = newBox
    }

    func blur()
    {
        var blur = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
        blur.frame = self.view.frame
        self.view.addSubview(blur)
    }

【问题讨论】:

    标签: uiview swift


    【解决方案1】:

    部分问题在于您无法知道addBox 是否在做任何事情。这是您的代码:

        let newBox = UIView(frame: location)
        newBox.backgroundColor = UIColor.clearColor()
        self.view.insertSubview(newBox, atIndex: 1)
    

    只包含清晰背景颜色的视图是完全不可见的。所以你什么也看不到 - 正如夏洛克·福尔摩斯所说,这正是你可能期望看到的。

    【讨论】:

    • 还不错。我希望每次添加子视图时都能获得五分钱,但我没有给它一个框架!所以它的大小为零,所以它是不可见的。我经常这样做,我现在很有钱......
    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 2011-09-05
    相关资源
    最近更新 更多