本节再来学习三个控件:UISwitch,UISegmentedControl,UITextField
注意:有些非常简单的 或 很少使用的方法/属性,本文没有提到。
import UIKit
class ViewController:UIViewController,UITextFieldDelegate{
var textField:UITextField!
overridefunc viewDidLoad() {
super.viewDidLoad()
let mySwitch =UISwitch() //就是开关按钮
//switch的宽高不能自定义,所以直接用下面方法确定x,y
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
//还有textColor,font,backgroud(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 //这里board的frame是以系统键盘左上角为原点,”摞“上面的
textField.delegate =self
view.addSubview(textField)
view.addSubview(seg)
view.addSubview(mySwitch)
}
// 键盘自收回(UITableView 里不能用,原因参考我的文章 [iOS 理解] 响应者链 ):
overridefunc touchesBegan(_ touches:Set<UITouch>, with event:UIEvent?) {
textField.resignFirstResponder()
}
// 常用的协议事件:
func textField(_ textField:UITextField, shouldChangeCharactersIn range:NSRange, replacementString string:String) -> Bool {
print(string) // string就是新输入的字符
return true //返回false,则输入不进去
}
func textFieldDidBeginEditing(_ textField:UITextField) {
// 已进入编辑状态
}
func textFieldShouldClear(_ textField:UITextField) -> Bool {
return true //false则点小错号不清内容
}
func textFieldShouldReturn(_ textField:UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
// 响应动作事件
func switched(swi:UISwitch){
if swi.isOn {
print(1)
}else{
print(2)
}
}
func segControl(seg:UISegmentedControl){
print(1)
}
}
下一篇:UISearchBar使用方法