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的宽高不能自定义,所以直接用下面方法确定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)
}
//键盘自收回:
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使用方法
- iOS UISwitch UISegmentedControl UITextField使用方法
- Swift UITextField,UITextView,UISegmentedControl,UISwitch
- UIButton、UITextField、UISlider、UISwitch、UISegmentedControl
- iOS开发——基本控件(UIButton,UILabel,UITextField,UISwitch,UISlider,UISegmentedControl)
- IOS入门学习笔记(普通UI控件--UISwitch、UITextField、UITextView、UISegmentedControl)
- iOS分段控制器UISegmentedControl和开关UISwitch
- iOS - UISegmentedControl使用方法
- iOS UISwitch和UITextField 的监听
- iOS自学笔记之UISwitch和UITextField
- iOS--UISwitch的使用方法详细
- iOS开发 UISwitch的使用方法
- iOS--UISegmentedControl的使用方法详细
- IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl
- IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl
- IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl
- IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl
- IOS7UISlider/UIProgressView/UISwitch/UISegmentedControl
- UISegmentedControl、UISwitch、UISlider
- Gson全解析之二:JsonReader的其它方法
- http://www.cnblogs.com/lonelywolfmoutain/p/4690512.html
- 第三周作业
- JS的循环语句
- x86汇编如何延时
- iOS UISwitch UISegmentedControl UITextField使用方法
- Unity&网络--HLAPI(1):服务器同步变量,‘脏标’模式以及[SyncVar]特性
- Linux下的进度条
- R语言入门(4)时间序列分析
- Dubbo之——Eclipse调试Dubbox源码
- spring事物的五种配置
- Linux中的文件描述符与打开文件之间的关系
- Linux经典问题—五哲学家就餐问题
- $watch--angularJs的监听事件