iOS UISwitch UISegmentedControl UITextField使用方法

来源:互联网 发布:北京上游网络怎么样 编辑:程序博客网 时间:2024/06/08 06:29

本节再来讲三个控件:UISwitch,UISegmentedControl,UITextField

注意:有些方法或属性 非常简单,本文没有提到。另有些很少使用的方法 日后会推文  :)


import UIKit


class ViewController:UIViewController,UITextFieldDelegate{

    var textField:UITextField!

    overridefunc viewDidLoad() {

        super.viewDidLoad()

        let mySwitch =UISwitch()

        //switch的宽高不能自定义,所以直接用下面方法确定xy

        mySwitch.frame.origin.x =100

        mySwitch.frame.origin.y =50

//      mySwitch.onTintColor = UIColor.lightGray    //打开时里面填充的颜色

//      mySwitch.thumbTintColor = UIColor.red //圆圈的颜色

        mySwitch.tintColor =UIColor.blue  //很难描述,和其他控件的tintColor差不多,渲染效果的色

        mySwitch.isOn =true

        mySwitch.addTarget(self, action:#selector(switched), for: .valueChanged)

        

        

        

        //segmentControl就是打开拨号,最上面有个:所有电话和未接电话连一块的按钮,就是那个

        let seg =UISegmentedControl(frame:CGRect(x:15, y:100, width:view.bounds.width-30, height:20))

        //下面的at,如果没有0,就按insert的顺序排列;如果有0,就按位置小大排列,中间没有空位

        seg.insertSegment(withTitle:"AAAAAA", at:2, animated:true)

        seg.insertSegment(withTitle:"B", at:0, animated:true)

        seg.addTarget(self, action:#selector(segControl), for: .valueChanged)

//      seg.isMomentary = true           //  true:不能选中,即不再点后填充满蓝色,和button一样了

//      seg.selectedSegmentIndex = 0    //定刚开始选中哪个

        seg.tintColor =UIColor.red

        seg.setBackgroundImage(UIImage(named:"3.png"), for: .normal, barMetrics: .default)

//seg title很长时,可设置自适应:

        seg.apportionsSegmentWidthsByContent =true

//一些设置:   setWidth值,某位置) setContentOffset(·,·)  setEnabled(·,·)这里不写了

        

        

        textField =UITextField(frame:CGRect(x: 10, y:300, width:view.bounds.width-50, height:30))

        textField.borderStyle = .roundedRect

        textField.placeholder ="Input here:"

        textField.textAlignment = .center

//还有textColorfontbackgroud(Color), disabledBackground,enabled 这里不写了

//      textField.clearsOnInsertion = true       //下面三行自己猜意思

//      textField.clearsOnBeginEditing = true

        textField.adjustsFontSizeToFitWidth =true

        

//      textField添加左边一个视图

        let left =UIView(frame:CGRect(x:0, y:0, width:30, height:30))

        left.backgroundColor =UIColor.lightGray

        textField.leftView = left

        textField.leftViewMode = .whileEditing

        textField.clearButtonMode = .whileEditing//右边的小错号

//      自定义输入键盘,位置是相对于原系统键盘位置

        let board =UIView(frame:CGRect(x:0, y:0, width:view.bounds.width ,height:30))

        board.backgroundColor =UIColor.lightGray

//      textField.inputView = board

//      或者加辅助键盘:

        textField.inputAccessoryView = board //这里boardframe是以系统键盘左上角为原点,上面的

        textField.delegate =self

        

        

        view.addSubview(textField)

        view.addSubview(seg)

        view.addSubview(mySwitch)

    }

    //键盘自收回:

    overridefunc touchesBegan(_ touches:Set<UITouch>, with event:UIEvent?) {

        textField.resignFirstResponder()

    }

    

//    协议事件:

    

    func textField(_ textField:UITextField, shouldChangeCharactersIn range:NSRange, replacementString string:String) ->Bool {

        print(string) //这个方法和上节searchbar的一个方法差不多,string就是新加的字符

        returntrue     //返回false,则输入不进去

    }

    func textFieldDidBeginEditing(_ textField:UITextField) {

        //其他几个相似方法参考 searchBar文章详细内容

    }

    func textFieldShouldClear(_ textField:UITextField) ->Bool {

        returntrue  //false则点小错号不清内容

    }

    func textFieldShouldReturn(_ textField:UITextField) ->Bool {

        textField.resignFirstResponder()

        returntrue

    }

    

    

//    响应动作事件

    func switched(swi:UISwitch){

        if swi.isOn {

            print(1)

        }else{

            print(2)

        }

    }

    func segControl(seg:UISegmentedControl){

        print(1)

    }

    

}

有些方法或属性 非常简单,本文没有提到。另有些很少使用的方法 日后会推文  :)

下一篇:UISearchBar使用方法






注意:有些方法或属性 非常简单,本文没有提到。另有些很少使用的方法 日后会推文  :)
0 0