IOS UIKit基础控件的使用

基础控件——UILabel

import UIKit

class ViewController: UIViewController {
    
    
    
    var text:UILabel!

    override func viewDidLoad() {
    
    
        super.viewDidLoad()

        let label:UILabel = UILabel(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        label.text = "hello world hello world hello world"
//        label.font = UIFont.boldSystemFont(ofSize: 25)
//        label.textColor = .blue
//        label.textColor = UIColor.systemRed
        label.textColor = UIColor.init(red:255, green: 0.0, blue: 0.0, alpha: 1)
        label.backgroundColor = .blue
        label.textAlignment = .center
        label.shadowColor = .orange
        label.shadowOffset = CGSize(width: 10, height: 10)
        label.numberOfLines = 2
        self.view.addSubview(label)
    }
}

基础控件——UIButton

import UIKit

class ViewController: UIViewController {
    
    
    
    var text:UILabel!

    override func viewDidLoad() {
    
    
        super.viewDidLoad()

    
        let btn:UIButton = UIButton(type: .custom)
        
        btn.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        
        btn.backgroundColor = UIColor.blue
        
        btn.setTitle("button", for: .normal)
        
        btn.contentEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 0)

        btn.setBackgroundImage(UIImage(named: "1.jpg"), for: .normal)
        btn.addTarget(self, action: #selector(click), for: UIControl.Event.touchUpInside)
        
        btn.showsTouchWhenHighlighted = true
      
        self.view.addSubview(btn)
    }
    
    @objc func click(btn:UIButton){
    
    
       
        btn.backgroundColor = .red
        print("clike me")
   
    }
    
}

基础控件——UIImageView

import UIKit

class ViewController: UIViewController {
    
    
    
    var text:UILabel!

    override func viewDidLoad() {
    
    
        super.viewDidLoad()

    
        let img:UIImageView = UIImageView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        img.image = UIImage(named: "1.jpg")
        img.backgroundColor = .gray
        //  设置高亮状态下的图片
        img.highlightedImage = UIImage(named: "1.jpg")
        img.isHighlighted = true
        self.view.addSubview(img)
    }
    
    
}


控件中layer的使用

以UIImageView为例

PS: 圆角样式和阴影不能同时使用。masksToBounds会把阴影效果去掉。

import UIKit

class ViewController: UIViewController {
    
    
    
    var text:UILabel!

    override func viewDidLoad() {
    
    
        super.viewDidLoad()

    
        let img:UIImageView = UIImageView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        img.image = UIImage(named: "1.jpg")

        // set radius

        img.layer.masksToBounds = true
        img.layer.cornerRadius = 50
        
        // set border color and boder width
        img.layer.borderWidth = 2
        img.layer.borderColor = UIColor.green.cgColor
        
        // set shadow
        img.layer.shadowColor = UIColor.purple.cgColor
        img.layer.shadowOffset = CGSize(width: 10, height: 10)
        img.layer.shadowOpacity = 1
        
        self.view.addSubview(img)
    }
    
    
}

基础控件——UISearchBar

import UIKit

class ViewController: UIViewController {
    
    
    
    var text:UILabel!

    override func viewDidLoad() {
    
    
        super.viewDidLoad()

    
        let searchBar:UISearchBar = UISearchBar(frame: CGRect(x: 20, y: 100, width: 280, height: 60))
//        searchBar.text = "sherlock"
        searchBar.placeholder = "请输入搜索内容"
        searchBar.barStyle = .default
//        searchBar.prompt = "title text"
        
        searchBar.showsSearchResultsButton = true
        searchBar.showsBookmarkButton = true
        
        searchBar.tintColor = .red
        
        searchBar.barTintColor = .blue
        
        searchBar.scopeButtonTitles = ["111","222","33333","455555"]
        searchBar.selectedScopeButtonIndex = 3
        searchBar.setShowsScope(true, animated: true)
        
        self.view.addSubview(searchBar)
    }
    
    
}

代理的简单使用

import UIKit

class ViewController: UIViewController,UISearchBarDelegate {
    
    
    
    var searchBar:UISearchBar!

    override func viewDidLoad() {
    
    
        super.viewDidLoad()

    
        searchBar = UISearchBar(frame: CGRect(x: 20, y: 100, width: 280, height: 60))
//        searchBar.text = "sherlock"
        searchBar.placeholder = "请输入搜索内容"
        searchBar.barStyle = .default
//        searchBar.prompt = "title text"
        
        searchBar.showsSearchResultsButton = true
        searchBar.showsBookmarkButton = true
        
        searchBar.tintColor = .red
        
        searchBar.barTintColor = .blue
        
        searchBar.scopeButtonTitles = ["111","222","33333","455555"]
        searchBar.selectedScopeButtonIndex = 3
        searchBar.setShowsScope(true, animated: true)
//        设置代理
        searchBar.delegate = self
        
        
        self.view.addSubview(searchBar)
    }
    
    
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    
//            焦点聚集时键盘自动弹出,点击其他空白区域,键盘隐藏起来
        searchBar.resignFirstResponder()
        }
    
//    点击附件视图时代理方法的回调
    func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
    
    
        print(selectedScope)
    }
//    当输入框文字变化时调用
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    
    
            print(searchText)
    }
    
    
}


基础控件——UISwitch

import UIKit

class ViewController: UIViewController {
    
    
    
  

    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        
//        大小系统默认的,所以width和height设置了也不会生效
        let mySwitch:UISwitch = UISwitch(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        
//        自定义UI
//        设置滑块颜色
        mySwitch.thumbTintColor = .red
//        设置开关开启的颜色
        mySwitch.onTintColor = .blue

        
        mySwitch.addTarget(self, action: #selector(switchClick), for: .valueChanged)
        
        self.view.addSubview(mySwitch)
        
    }
    
    @objc func switchClick(mySwitch:UISwitch) {
    
    
        if mySwitch.isOn {
    
    
            self.view.backgroundColor = .blue
        }else {
    
    
            self.view.backgroundColor = .white
        }
    }
    
}


基础控件——UISegmentController

import UIKit

class ViewController: UIViewController {
    
    
    
    var seg:UISegmentedControl!
  

    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        
        seg = UISegmentedControl(frame: CGRect(x: 20, y: 100, width: 280, height: 40))
        
//        设置分段控制器的按钮
        seg.insertSegment(withTitle: "第一个按钮", at: 0, animated: true)
        seg.insertSegment(withTitle: "第二个按钮", at: 1, animated: true)
        
//        添加点击事件
        seg.addTarget(self, action: #selector(clickSeg), for: .valueChanged)
        
//        设置分段控制器是否保存选中状态  默认为false
        seg.isMomentary = false
        
//        设置按钮宽度自适应
//        seg.apportionsSegmentWidthsByContent = true
        
//        手动设置按钮宽度
//        seg.setWidth(40, forSegmentAt: 0)
        
//        设置内容区域的偏移量
//        seg.setContentOffset(CGSize(width: 10, height: 10), forSegmentAt: 0)
        
//        设置默认选中
        seg.selectedSegmentIndex = 0
        
//        设置背景颜色
        seg.backgroundColor = .blue
        
        
        self.view.addSubview(seg)
    }
    
    @objc func clickSeg(seg:UISegmentedControl){
    
    
        print(seg.selectedSegmentIndex)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    
//        seg.insertSegment(withTitle: "第三个按钮", at: 2, animated: true)
    }
 

使用NavigationController和UISegmentController实现简单的页面切换

import UIKit

class ViewController: UIViewController {
    
    
    
    var width:CGFloat!
    var height:CGFloat!
    var viewBlue:UIView!
    var viewRed:UIView!
    
    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        width = self.view.frame.width
        height = self.view.frame.height
        viewBlue = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height))
        viewBlue.backgroundColor = .blue
        viewRed = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height))
        viewRed.backgroundColor = .red
        let seg:UISegmentedControl = UISegmentedControl(items: ["red","blue"])

        seg.setWidth(100, forSegmentAt: 0)
        seg.setWidth(100, forSegmentAt: 1)
        
        seg.addTarget(self, action: #selector(change), for: .valueChanged)
        
        seg.selectedSegmentIndex = 0
        
        self.navigationItem.titleView = seg
            
        
        
        self.view.addSubview(viewBlue)
        self.view.addSubview(viewRed)
        self.view.bringSubviewToFront(viewBlue)
    }
    
    @objc func change(seg:UISegmentedControl){
    
    
        if seg.selectedSegmentIndex == 0{
    
    
            self.view.bringSubviewToFront(viewRed)
        }else {
    
    
            self.view.bringSubviewToFront(viewBlue)
        }
    }

 
}

基础控件——UITextField

import UIKit

class ViewController: UIViewController {
    
    
    let textField:UITextField = UITextField(frame: CGRect(x: 20, y: 100, width: 280, height: 40))
    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        
        
//        设置背景颜色
//        textField.backgroundColor = .red
        
//        设置输入框风格
        textField.borderStyle = .bezel
        textField.borderStyle = .line
        
//        设置输入框的文字提示
        textField.placeholder = "请输入姓名"
        
//        设置输入文字的颜色
        textField.textColor = .blue
//        设置文字字体
        textField.font = .italicSystemFont(ofSize: 15)
        
//        设置字体对齐方式
        textField.textAlignment = .center
        
//        设置每次开始编辑时,是否清空输入框的内容
        textField.clearsOnBeginEditing = true
        
//        设置是否可编辑
//        textField.isEnabled = false
        
        
//        设置输入框的左视图
        let viewLeft = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        viewLeft.backgroundColor = .red
//        设置输入框的右视图
        let viewRight = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        viewRight.backgroundColor = .blue
        
        textField.leftView = viewLeft
        textField.rightView = viewRight
//        设置左右视图的模式
        textField.leftViewMode = .always
        textField.rightViewMode = .always
        
        
//        设置弹出的交互键盘
        let board:UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
//        textField.inputView = board
        
//        设置附键盘视图
        board .backgroundColor = .red
        textField.inputAccessoryView = board
        
        self.view.addSubview(textField)
        
    }

    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    
        textField.resignFirstResponder()
    }

}

UITextField代理方法的使用

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {
    
    
    let textField:UITextField = UITextField(frame: CGRect(x: 20, y: 100, width: 280, height: 40))
    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        
        
//        设置背景颜色
//        textField.backgroundColor = .red
        
//        设置输入框风格
        textField.borderStyle = .bezel
        textField.borderStyle = .line
        
//        设置输入框的文字提示
        textField.placeholder = "请输入姓名"
        
//        设置输入文字的颜色
        textField.textColor = .blue
//        设置文字字体
        textField.font = .italicSystemFont(ofSize: 15)
        
//        设置字体对齐方式
        textField.textAlignment = .center
        
//        设置每次开始编辑时,是否清空输入框的内容
//        textField.clearsOnBeginEditing = true
        
//        设置是否可编辑
//        textField.isEnabled = false
        
        
//        设置输入框的左视图
        let viewLeft = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        viewLeft.backgroundColor = .red
//        设置输入框的右视图
        let viewRight = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        viewRight.backgroundColor = .blue
        
        textField.leftView = viewLeft
        textField.rightView = viewRight
//        设置左右视图的模式
        textField.leftViewMode = .always
//        textField.rightViewMode = .always
        
        textField.clearButtonMode = .always
//        设置弹出的交互键盘
        let board:UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
//        textField.inputView = board
        
//        设置附键盘视图
        board .backgroundColor = .red
        textField.inputAccessoryView = board
        
        
//        设置代理
        textField.delegate = self
        
        
      
        self.view.addSubview(textField)
        
    }

    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    
        textField.resignFirstResponder()
    }

    // 当输入时调用的方法
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    
        print(string)
        return true
    }
    
//    编辑结束时调用的方法
    func textFieldDidEndEditing(_ textField: UITextField) {
    
    
        print("end")
    }
//    编辑开始时调用的方法
    func textFieldDidBeginEditing(_ textField: UITextField) {
    
    
        print("begin")
    }
//    将要进入编辑状态时调用的方法
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    
    
        print("将要进入编辑状态")
        return true
    }
    
//    将要结束编辑状态时调用的方法
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    
    
        print("将要结束编辑状态")
        return true
    }
    
    
//    输入的内容是否可以被清除
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
    
    
        return true
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    
    
        textField.resignFirstResponder()
        return true
    }
    
}

基础控件——UISlider

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {
    
    

    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        
        let slider:UISlider = UISlider(frame: CGRect(x: 20, y: 100, width: 280, height: 40))
        slider.addTarget(self, action: #selector(sliderChange), for: .valueChanged)
        
//        当滑动停止时才触发valueChanged
        slider.isContinuous  = false
        
//        设置取值范围
        slider.maximumValue = 100
        slider.minimumValue = 0
        
//        设置滑动轴的颜色
        slider.minimumTrackTintColor = .red
        slider.maximumTrackTintColor = .blue
        
//        设置滑块的颜色
        slider.thumbTintColor  = .orange
        
        self.view.addSubview(slider)
        
    }
    
    @objc func sliderChange(slider:UISlider){
    
    
        print(slider.value)
    }
}

基础控件——UIActivatyIndactorView

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {
    
    

    var width:CGFloat!
    var height:CGFloat!
    
    
    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        width = self.view.frame.width
        height = self.view.frame.height
        
        
        let activity:UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: width/2-10, y: height/2, width: 20, height: 20))

        
        activity.style = .medium
        self.view.addSubview(activity)
//        开始活动
        activity.startAnimating()
//        停止活动
//        activity.stopAnimating()
        
        
        

    }
}

基础控件——UIPageController

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {
    
    

    var width:CGFloat!
    var height:CGFloat!
    
    
    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        width = self.view.frame.width
        height = self.view.frame.height
        self.view.backgroundColor = .orange
        
        let page:UIPageControl = UIPageControl(frame: CGRect(x: 100, y: 100, width: 200, height: 30))
        page.numberOfPages = 6
     
//        设置分页控制器的当前页颜色
//        page.currentPageIndicatorTintColor = .red
        
//        设置分页点颜色
//        page.pageIndicatorTintColor = .blue
        
        
        
        
//        设置当前页码
//        page.currentPage = 3
        
        page.addTarget(self, action: #selector(change), for: .valueChanged)
        
        self.view.addSubview(page)
        

    }
    
    
    @objc func change(page:UIPageControl) {
    
    
        print(page.currentPage)
    }
    
}

基础控件——UIStepper

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {
    
    

    var width:CGFloat!
    var height:CGFloat!
    
    
    let label:UILabel = UILabel(frame: CGRect(x: 100, y: 200, width: 100, height: 100))
    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        width = self.view.frame.width
        height = self.view.frame.height
        
        let stepper:UIStepper = UIStepper(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        
        

        
        label.text = String(format: "%f", stepper.value)
        
        
        stepper.addTarget(self, action: #selector(change), for: .valueChanged)
        
        self.view.addSubview(stepper)
        self.view.addSubview(label)
    
    }
    
    @objc func change(stepper:UIStepper) {
    
    
        label.text = String(format: "%f", stepper.value)
    }
    
}

基础控件——UIAlertController

import UIKit

class ViewController: UIViewController,UIAlertViewDelegate {
    
    

    var width:CGFloat!
    var height:CGFloat!
    
    
    let label:UILabel = UILabel(frame: CGRect(x: 100, y: 200, width: 100, height: 100))
    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        width = self.view.frame.width
        height = self.view.frame.height
        
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    
        
        let alertView:UIAlertController = UIAlertController(title: "alert", message: "hello workd", preferredStyle: .alert)
        
        let alertActionOne:UIAlertAction = UIAlertAction(title: "cancel", style: .default) {
    
     (UIAlertAction) ->Void in
            print("hello world");
        }
    
        alertView.addAction(alertActionOne)
        self.present(alertView, animated: true, completion: nil)
        
    }
    
}
import UIKit

class ViewController: UIViewController,UIAlertViewDelegate {
    
    

    var width:CGFloat!
    var height:CGFloat!
    
    
    let label:UILabel = UILabel(frame: CGRect(x: 100, y: 200, width: 100, height: 100))
    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        width = self.view.frame.width
        height = self.view.frame.height
        
    
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    
        
        
        let actionSheet:UIAlertController = UIAlertController(title: "actionSheet", message: "hello world", preferredStyle: .actionSheet)
        
        let actionAction:UIAlertAction = UIAlertAction(title: "default", style: .default) {
    
     (UIAlertAction) ->Void in
             print("one")
         }
        let actionActionOther:UIAlertAction = UIAlertAction(title: "cancel", style: .cancel) {
    
     (UIAlertAction) ->Void in
             print("other")
         }
        
        
        actionSheet.addAction(actionAction)
        actionSheet.addAction(actionActionOther)
        
        self.present(actionSheet, animated: true, completion: nil)
        
        
    }
    
}

猜你喜欢

转载自blog.csdn.net/qq_43058685/article/details/111697226