关于ios:ios-学习之基础-UI-组件-一

41次阅读

共计 7164 个字符,预计需要花费 18 分钟才能阅读完成。

开始

你认为学完 swift 之后就能够开发 App 啦?还早呢,UI 组件学一学吧 ????

UILabel

    // 应用代码增加 UILabel 横向为 X,纵向为 Y,坐标原点在左上角
    let label = UILabel(frame: CGRect(x: 20, y: 40, width: 200, height: 30))
    label.text = "Hello World"
    // 增加到以后视图
    self.view.addSubview(label)

    let label2 = UILabel(frame: CGRect(x: 20, y: 60, width: 200, height: 30))
    label2.text = "Geer"

    // 设置字体为 20 号加粗的零碎字体
    label2.font = UIFont.boldSystemFont(ofSize: 20)

    // 设置字体色彩为红色
    label2.textColor = UIColor.red

    // 设置暗影色彩
    label2.shadowColor = UIColor.green

    // 设置暗影偏移
    label2.shadowOffset = CGSize(width: 2, height: 2)

    // 设置字体对其
    label2.textAlignment = NSTextAlignment.center
    self.view.addSubview(label2)

    // 多行文本
    let text = """除了自定义连贯表的表名,你还能够通过传递额定的参数到 belongsToMany 办法来定义该表中字段的键名。第三个参数是定义此关联的模型在连贯表里的外键名,第四个参数是另一个模型在连贯表里的外键名:"""
    let label3 = UILabel(frame: CGRect(x: 20, y: 80, width: 200, height: 150))
    label3.text = text

    // 设置显示行数
    label3.numberOfLines = 7
    self.view.addSubview(label3)

UIButton

        // 创立 button 实例,指定 button 的类型
        let button = UIButton(type: .system)
        
        // 设置地位与尺寸
        button.frame = CGRect(x: 20, y: 40, width: 100, height: 30)
        
        // 设置按钮背景色彩
        button.backgroundColor = .purple
        
        // 设置按钮题目
        button.setTitle("我是按钮", for: UIControl.State())
        
        // 设置题目背景色彩
        button.setTitleColor(.white, for: UIControl.State())
        
        // 为按钮增加事件
        // 第一个传入触发事件时事件办法的执行者,比方以后则是 self
        // 第二个参数传入一个 #selector 选择器实例,这个参数决定要执行的办法
        // 第三个参数设置触发的条件,列入按钮被按下,按钮被按下并抬起
        
        button.addTarget(self, action: #selector(touchBegin), for: UIControl.Event.touchUpInside)
        
        self.view.addSubview(button)
@objc func touchBegin(){print("click button")
    }

UIImageView

        // 创立图片实例
        let image = UIImage(named: "img1")
        
        // 创立图片 View
        let imageView = UIImageView(image: image)
        
        // 设置 view 的地位
        imageView.frame = CGRect(x: 40, y: 50, width: 200, height: 200)
        
        self.view.addSubview(imageView)
        
        // 动画
        var imageArray = Array<UIImage>()
        
        for index in 1...2 {if let fImage = UIImage(named: "f\(index)") {imageArray.append(fImage)
            }
        }
        
        // 创立 View
        let fImageView = UIImageView(frame: CGRect(x: 30, y: 250, width: 200, height: 100))
        
        // 设置动画数组
        fImageView.animationImages = imageArray
        
        // 设置播放时长
        fImageView.animationDuration = 3
        
        // 设置动画播放次数
        fImageView.animationRepeatCount = 0
        
        self.view.addSubview(fImageView)
        
        // 开始播放
        fImageView.startAnimating()

UITextField

        // 创立实例 设置尺寸
        let textField = UITextField(frame: CGRect(x: 20, y: 30, width: 100, height: 30))
        
        // 设置边框输出格调
        textField.borderStyle = .line
        
        // 设置文字色彩
        textField.textColor = .red
        
        // 设置对齐形式
        textField.textAlignment = .center
        
        // 设置提醒文字
        textField.placeholder = "请输出姓名"
        
        // 设置代理为自身
        textField.delegate = self
        
        self.view.addSubview(textField)
        
        
        
        let textField2 = UITextField(frame: CGRect(x: 20, y: 90, width: 100, height: 30))
        
        // 设置边框输出格调
        textField2.borderStyle = .line
        
        // 设置文字色彩
        textField2.textColor = .red
        
        // 设置对齐形式
        textField2.textAlignment = .center
        
        // 设置提醒文字
        textField2.placeholder = "请输出姓名"
        
        
        // 左视图
        let imageView1 = UIImageView(image: UIImage(named: "img1"))
        imageView1.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        
        // 右视图
        let imageView2 = UIImageView(image: UIImage(named: "f1"))
        imageView2.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        
        
        // 设置左右视图
        textField2.leftView = imageView1
        textField2.rightView = imageView2
        
        // 设置视图模式
        textField2.leftViewMode = .always
        textField2.rightViewMode = .always
        
        self.view.addSubview(textField2)

代理办法

// 在输入框行将进入编辑状态时调用
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {print("行将进入编辑状态时调用")
        return true
    }
    
    // 曾经开始编辑时调用
    func textFieldDidBeginEditing(_ textField: UITextField) {print("曾经开始编辑时调用")
    }
    
    // 按 return 时调用
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {textField.resignFirstResponder()
        return true
    }

UISwitch

        let swi = UISwitch()
        swi.frame = CGRect(x: 20, y: 20, width: 100, height: 20)
        
        // 设置视图中心点的坐标
        swi.center = CGPoint(x: 100, y: 100)
        
        // 设置开启状态的色彩
        swi.onTintColor = .green
        
        // 设置一般状态的色彩
        swi.tintColor = .red
        
        // 设置滑动的色彩
        swi.thumbTintColor = .purple
        
        // 设置开关的初始状态
        swi.isOn = true
        
        swi.addTarget(self, action: #selector(switchChange), for: UIControl.Event.valueChanged)
        self.view.addSubview(swi)
@objc func switchChange(swi: UISwitch){print(swi.isOn)
    }

UIPageController

        // 创立
        let pageControl = UIPageControl(frame: CGRect(x: 20, y: 100, width: 280, height: 30))
        
        // 设置页数
        pageControl.numberOfPages = 10
        
        // 设置背景色彩
        pageControl.backgroundColor = .red
        
        // 设置页码点背景色彩
        pageControl.pageIndicatorTintColor = .green
        
        // 设置选中的页码点色彩
        pageControl.currentPageIndicatorTintColor = .blue
        
        // 设置以后选中的页数
        pageControl.currentPage = 3
        
        pageControl.addTarget(self, action: #selector(uiPageChange), for: UIControl.Event.valueChanged)
        self.view.addSubview(pageControl)
    @objc func uiPageChange(pageControl: UIPageControl){print("以后 \(pageControl.currentPage)")
    }

UISegmentedControl

        let items = ["按钮 1","按钮 2","按钮三"]
        let segmentedControl = UISegmentedControl(items: items)
        
        // 设置地位与尺寸
        segmentedControl.frame = CGRect(x: 100, y: 100, width: 200, height: 30)
        
        // 设置控件格调与色彩
        segmentedControl.tintColor = .blue
        
        segmentedControl.addTarget(self, action: #selector(segmentedControlChange), for: UIControl.Event.valueChanged)
        
        self.view.addSubview(segmentedControl)
@objc func segmentedControlChange(seg: UISegmentedControl){print("抉择了 \(seg.selectedSegmentIndex)")
        // 插入新的按钮
        seg.insertSegment(withTitle: "按钮 5", at: 0, animated: true)
    }

UISlider

        let slider = UISlider(frame: CGRect(x: 20, y: 100, width: 280, height: 30))
        
        // 设置最小值
        slider.minimumValue = 0
        slider.maximumValue = 10
        
        // 初始滑块的值
        slider.value = 5
        
        // 设置滑块左侧进度条的色彩
        slider.minimumTrackTintColor = .red
        
        // 设置滑块右侧的色彩
        slider.maximumTrackTintColor = .green
        
        // 设置滑块的色彩
        slider.thumbTintColor = .blue
        
        // 设置进行滑动才触发事件
        slider.isContinuous = false
        
        slider.addTarget(self, action: #selector(uISliderChange), for: UIControl.Event.valueChanged)
        
        self.view.addSubview(slider)
@objc func uISliderChange(slider: UISlider){print(slider.value)
    }

UIActivityIndicatorView

        let activity = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.medium)
        
        activity.center = self.view.center
        activity.color = .blue
        
        // 开始播放
        activity.startAnimating()
        
        self.view.addSubview(activity)

UIProgressView

        let progressView = UIProgressView(progressViewStyle: UIProgressView.Style.default)
        
        // 设置地位和宽度
        progressView.frame = CGRect(x: 20, y: 100, width: 280, height: 30)
        
        // 设置以后进度
        progressView.progress = 0.5
        
        // 设置已走过的进度条进度
        progressView.progressTintColor = .red
        
        // 设置未走过的色彩
        progressView.trackTintColor = .blue
        
        self.view.addSubview(progressView)

UIStepper

        let steper = UIStepper(frame: CGRect(x: 100, y: 100, width: 0, height: 0))
        // 设置背景色彩
        steper.backgroundColor = UIColor.red
        
        // 设置控件色彩
        
        steper.tintColor = .blue
        
        // 设置最小值
        steper.minimumValue = 0
        steper.maximumValue = 10
        
        // 设置控件的步长
        steper.stepValue = 1
        
        self.view.addSubview(steper)

UIPickerView

        let pickerView = UIPickerView(frame: CGRect(x: 20, y: 100, width: 280, height: 200))
        
        // 设置代理 须要增加 UIPickerViewDelegate
        pickerView.delegate = self
        
        // 设置数据源 须要增加 UIPickerViewDataSource
        pickerView.dataSource = self
        
        self.view.addSubview(pickerView)

代理

    // pickView 此办法须要选择器控件的分组数量
    func numberOfComponents(in pickerView: UIPickerView) -> Int {return 2}
    
    // 此办法须要返回每个分组的行数
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {return 10}
    
    // 此办法返回每个分组中的每行数据的题目
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {return "第 \(component + 1) 组第 \(row) 行"
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {print("用户抉择了 \(component) 组 \(row) 行")
    }

UIDataPicker

        let dataPicker = UIDatePicker(frame: CGRect(x: 20, y: 100, width: 280, height: 200))
        
        // 设置工夫选择器的模式
        dataPicker.datePickerMode = .dateAndTime
        dataPicker.addTarget(self, action: #selector(uiDataPickerChange), for: UIControl.Event.valueChanged)
        self.view.addSubview(dataPicker)
@objc func uiDataPickerChange(dataPicker: UIDatePicker){print(dataPicker.date)
    }

UISearchBar

let searchBar = UISearchBar(frame: CGRect(x: 20, y: 100, width: 280, height: 30))
        
        // 设置控件的格调
        searchBar.searchBarStyle = .minimal
        
        searchBar.placeholder = "请输出商品题目"
        
        // 显示勾销按钮
        searchBar.showsCancelButton = true
        
        // 显示控件的书库按钮
        searchBar.showsBookmarkButton = true
        
        self.view.addSubview(searchBar)

完结

今日先学这么多,今天再持续

正文完
 0