简体   繁体   中英

Swift UIView Custom Position

Sorry in advance for noob question:

I have function below that works perfectly with one catch this view covers a button i have on the bottom of the screen. How can I adjust this function to work as is only to leave 50 px of space on the bottom of the screen for the button. Is it possible to use mainScreen().bounds and just leave some space at top or bottom?

func flashColor() {
    if let window = self.view{
        let screenSize: CGRect = UIScreen.mainScreen().bounds
        let v = UIView(frame: screenSize)

        v.backgroundColor = colors[i]
        v.alpha = 1

        if i < 4 {
            i += 1
        } else {
            i = 0
        }

        window.addSubview(v)
        UIView.animateWithDuration(1, animations: {
            v.alpha = 0.0
            }, completion: {(finished:Bool) in
                print(i)
                v.removeFromSuperview()
        })

    }
}

In general, it is best to avoid using UIScreen.mainScreen().bounds as this will return the bounds for the entire device screen. While before split screen multitasking it was generally okay to assume that your app would take over the entire screen, that is now longer the case. You can use your view controller's view's bounds instead. If you want a view to cover your view controller's view from top to 50 points above the bottom you can say something like:

func flashColor() {
    if let vcView = self.view{
        let viewSize: CGRect = CGRect(x:0.0,y:0.0,width:vcView.bounds.size.width,height:vcView.bounds.size.height - 50.0)
        let v = UIView(frame: viewSize)

        v.backgroundColor = colors[i]
        v.alpha = 1

        if i < 4 {
            i += 1
        } else {
            i = 0
        }

        window.addSubview(v)
        UIView.animateWithDuration(1, animations: {
            v.alpha = 0.0
            }, completion: {(finished:Bool) in
                print(i)
                v.removeFromSuperview()
        })

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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