iOS 11开发教程(十六)iOS11应用视图之删除空白视图

iOS 11开发教程(十六)iOS11应用视图之删除空白视图

当开发者再也不须要主视图的某一视图时,能够将该视图删除。实现此功能须要使用到removeFromSuperview()方法,其语法形式以下:ide

要删除的视图对象名.removeFromSuperview()spa

【示例1-3】如下代码将在主视图中添加两个视图,而后再使用removeFromSuperview()方法删除其中一个视图。代码以下:对象

import UIKit教程

class ViewController: UIViewController {开发

    override func viewDidLoad() {rem

        super.viewDidLoad()it

        // Do any additional setup after loading the view, typically from a nib.io

        //添加空白视图newView1class

        let newView1=UIView(frame: CGRect(x: 0, y: 75, width: 375, height: 232))import

        newView1.backgroundColor=UIColor.cyan

        self.view.addSubview(newView1)

        //添加空白视图newView2

        let newView2=UIView(frame: CGRect(x: 0, y: 352, width: 375, height: 232))

        newView2.backgroundColor=UIColor.orange

        self.view.addSubview(newView2)

    }

……

}

此时运行程序,会看到如图1.54所示的效果。若是想要删除视图对象newView1的话,须要使用removeFromSuperview()方法,代码以下:

newView1.removeFromSuperview()                                                               //删除视图对象newView1

运行效果如图1.55所示。

 

图1.54  运行效果                            图1.55  运行效果

相关文章
相关标签/搜索