ios - Swift UIView 自定义位置

标签 ios swift

对于菜鸟问题​​提前抱歉:

我有下面的功能,可以完美地与一个捕获此 View 覆盖我在屏幕底部的一个按钮。我如何调整此功能以仅在屏幕底部为按钮留出 50 px 的空间。是否可以使用 mainScreen().bounds 并在顶部或底部留一些空间?

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

    }
}

最佳答案

一般来说,最好避免使用 UIScreen.mainScreen().bounds,因为这将返回整个设备屏幕的边界。在分屏多任务处理之前,通常可以假设您的应用程序将接管整个屏幕,但现在情况不再如此。您可以改用 View Controller 的 View 边界。如果你想要一个 View 覆盖 View Controller 的 View ,从顶部到底部上方 50 点,你可以这样说:

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

    }
}

关于ios - Swift UIView 自定义位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36465026/

相关文章:

ios - UITableView 滚动快速更改单元格背景颜色

ios - 调用 setWorldOrigin 时子节点不可见?

ios - XCode7 : "Failed to render instance of" affects all UICollectionViewCells

ios - 导航栏的标题隐藏在后面的标题后面

ios - 如何使用两个查询填充一个 UITableView?

swift - JSONObjectWithData错误: unexpectedly found nil while unwrapping an Optional value

ios - 为什么 iOS 中的 slider 最小值图像变得模糊?

ios - 在 swift 中存储一个 for in 循环

swift - 在不初始化变量的情况下执行n次代码行

ios - TableView 内容未显示