在Swift中加载视图

时间:2017-11-04 19:41:22

标签: ios swift loadview

快速问题!对不起,请提前告知。我刚开始开发iOS应用程序,并希望加载一个单击按钮的视图。我在swift4工作。

    public static int search(int[] a, int target) {
    for (int i = 0; i < a.length; i++) {
        if (a[i] == target) {
            return i;
        }
    }
    return -1;
}

当前错误是“编辑器是占位符”,用于obj_LoadView = LoadView(frame:&lt; #CGRect#&gt;)。我知道我需要为CGRect添加一些东西,但不确定是什么。任何帮助表示赞赏,谢谢!

2 个答案:

答案 0 :(得分:2)

当您开始输入时,Xcode会为您自动填写语句。如果您不更改自动完成的参数,则会出现该错误。

问题是这一行:

obj_LoadView = LoadView(frame: <#CGRect#>)

<#CGRect#>需要使用实际的CGRect而不是占位符进行更改。

将CGRect设置为您想要新视图的大小:

obj_LoadView = LoadView(frame: CGRect(x: 0.0, y: 0.0, width: 400.0, height: 400))

这将使视图的框架为400x400点。

如果你想让视图成为视图的完整大小,你可以这样做:

obj_LoadView = LoadView(frame: view.frame)

答案 1 :(得分:1)

我个人喜欢创建自己的activityIndi​​catorView

&#13;
&#13;
class LoadingView: UIView {
    
    private var activityIndicatorView: UIActivityIndicatorView {
        
        let view = UIActivityIndicatorView(activityIndicatorStyle: .white)
        view.startAnimating()
        
        view.center = self.center
        return view
    }
    
    private var isPresenting: Bool = false
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .black
        self.alpha = 0.0
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func present() {
        guard let window = UIApplication.shared.keyWindow else { return }
        window.addSubview(self)
        fadeIn()
    }
    
    func dismiss() {
        guard isPresenting == true else { return }
        fadeOut()
    }
    
    private func fadeIn() {
        UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut, animations: {
            self.alpha = 0.5
        }) { (bool: Bool) in
            self.addSubview(self.activityIndicatorView)
            self.isPresenting = true
        }
    }
    
    private func fadeOut() {
        UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut, animations: {
            self.alpha = 0.0
        }) { (bool: Bool) in
            self.removeFromSuperview()
            self.isPresenting = false
        }
    }
}
&#13;
&#13;
&#13;