学习ios之基础控件
2017-03-25 01:09
148 查看
前言:其实一直从事的Android开发的工作,一直对ios开发很感兴趣,最近闲来无事决定就学习一下,相信对自己的成长和进步是有用的,由于我之前自学过c语言和oc和swift,语言方面不在赘述,其实你只要学会了一门语言其他的语言只不过是熟练的问题,能看能写就可以了。所以直接从ios开发的最基础部分UI部分开始记录,加深记忆,并且和各位共勉之。
一、UIView关系结构图
我们看下图有一个大概的印象,下面挨个的我拣着重要的和常用的讲,interface Builder部分就不讲了,大家可以自己查资料,还是比较简单的,我大部分会使用代码来讲解。
二、简单控件
1、标签和按钮
UILabel(相当于android中的TextView) 和UIButton(Android中的Button)
我们使用UILabel和UIButton实现一个简单的功能,当点击UIButton的时候,UILabel的内容会变成 Hello World!
首先由于我们使用纯代码构建应用,所以可以删除故事版文件,在Appdelete中指定RootControler文件
效果如图所示:
点击click me 按钮可以改变上面的文字
2、UITextField(EditText) UITextView(可以编辑的UILabel)
下面接着学习 UITextView 和UITextField 最后的效果图如下
直接看代码吧 都注释在里面了
Appdelegate没啥好说的省略了,直接看ViewController
代码里面都有注释,不需要详细说了,先会大概使用就好,真正的熟练还是要靠真正的项目
3、开关控件、分段控件和滑块控件
开关控件UISwitch(相当于android的switch或者使用selector的checkButton)
分段控件UISegmentedControl(相当于android RadioGroup或者TabLayout)
滑块控件 UISlider (相当于android的seekBar)
下面的例子效果图:
直接看代码:
4、Web视图:WKWebView类(相当于Android的WebView)
WKWebView是苹果ios8之后引入的新特性,用于替换旧的UIWebView,相对于之前更加的强大,我们来个简单的例子了解一下
效果图
5、警告框和操作表
警告框相当于Android的alertDialog
操作表 Android没有专门的控件,可以使用View或者popupWindow实现,也比较简单,不过貌似没有ios的封装的好
ios中警告框和操作表都是使用UIAlertController实现
咱们看下实际使用:
直接看代码吧都很简单,可以体会一下:
6、等待相关的控件与进度条
android中的progressBar可以设置style样式,显示是圆形进度条或者是条形进度条,但是ios是两种方式实现的,接下来咱们看一下
7、工具栏和导航栏
跟android中的toolbar比较像,废话不多说,直接上图,上代码
8、UIImageView
相当于Android中的ImageView 但是功能强大一点貌似,好像ios中的所有跟Android相似的控件功能都要更强大,蛋疼
效果图:
直接上代码,很简单,会简单运用就行
接下来学习高级控件,加油!~
一、UIView关系结构图
我们看下图有一个大概的印象,下面挨个的我拣着重要的和常用的讲,interface Builder部分就不讲了,大家可以自己查资料,还是比较简单的,我大部分会使用代码来讲解。
二、简单控件
1、标签和按钮
UILabel(相当于android中的TextView) 和UIButton(Android中的Button)
我们使用UILabel和UIButton实现一个简单的功能,当点击UIButton的时候,UILabel的内容会变成 Hello World!
首先由于我们使用纯代码构建应用,所以可以删除故事版文件,在Appdelete中指定RootControler文件
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // 设置显示界面的控制器是LabelAndButtonViewController //而不是我们创建的默认的故事版文件 self.window=UIWindow(frame:UIScreen.main.bounds) self.window?.rootViewController=LabelAndButtonViewController() self.window?.backgroundColor=UIColor.white self.window?.makeKeyAndVisible() return true } ... 其他的省略 }
import UIKit class LabelAndButtonViewController: UIViewController { var label:UILabel! var changeButton:UIButton! //视图加载完毕时 override func viewDidLoad() { super.viewDidLoad() // 初始化UILabel和UIButton let screen=UIScreen.main.bounds let labelWidth:CGFloat=100 let labelHeight:CGFloat=31 self.label=UILabel.init(frame: CGRect(x: (screen.size.width - labelWidth)/2, y: 90, width: labelWidth ,height: labelHeight)) self.label.text="点击按钮哦!" self.label.textAlignment=NSTextAlignment.center self.view.addSubview(self.label) //初始化UIButton let buttonWidth:CGFloat=60 let buttonHeight:CGFloat=31 self.changeButton=UIButton(type: UIButtonType.system) self.changeButton.frame=CGRect(x: (screen.size.width-buttonWidth)/2,y: 180,width: buttonWidth,height: buttonHeight) self.changeButton.setTitle("click me", for: UIControlState.normal) self.changeButton.addTarget(self, action: #selector(labe.changeContent(_:)), for: .touchUpInside) self.view.addSubview(self.changeButton) } /*内存不足警告*/ override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //改变UILabel的内容 func changeContent(_ sender: UIButton) { if(self.label.text=="Hello Word!"){ self.label.text="dear!"; }else{ self.label.text="Hello Word!"; } } }
效果如图所示:
点击click me 按钮可以改变上面的文字
2、UITextField(EditText) UITextView(可以编辑的UILabel)
下面接着学习 UITextView 和UITextField 最后的效果图如下
直接看代码吧 都注释在里面了
Appdelegate没啥好说的省略了,直接看ViewController
import UIKit //实现UITextFileld 和UITextView的委托协议,后面对于复杂控件还有数据源协议等,后面再说 class ViewController: UIViewController,UITextFieldDelegate,UITextViewDelegate { var textFiled:UITextField! var textView:UITextView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //获取屏幕CGRect let frame=UIScreen.main.bounds //初始化UILabel 默认字体大小和颜色 let topSpace:CGFloat=77 let spaceX:CGFloat=33 let name=UILabel.init(frame: CGRect(x: spaceX,y: topSpace,width: 90,height: 31)) name.text="Name:" name.textAlignment=NSTextAlignment.left//内容靠左边对齐 self.view.addSubview(name)//添加到UIView视图中 //初始化UITextfield self.textFiled=UITextField.init(frame: CGRect(x: spaceX,y: name.frame.origin.y+41,width: (frame.size.width-2*spaceX), height: 31 )) self.textFiled.clearButtonMode=UITextFieldViewMode.whileEditing self.textFiled.delegate=self //设置代理协议 self.textFiled.borderStyle=UITextBorderStyle.roundedRect//设置边框样式默认其实就是左边对齐 这样的可以不用写 self.view.addSubview(self.textFiled) //初始化UILabel和UITextview let abstract=UILabel.init(frame: CGRect(x: spaceX,y: self.textFiled.frame.origin.y+topSpace ,width: 90,height: 31 )) abstract.text="Abstract:" self.view.addSubview(abstract) self.textView=UITextView.init(frame: CGRect(x: spaceX,y: abstract.frame.origin.y+41,width: (frame.size.width-2*spaceX),height: 200)) self.textView.text="Hello! I am learning IOS !" self.textView.delegate=self //代理协议 self.view.addSubview(self.textView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated); //由于UITextFiled和UItextView都与键盘打交道,这里就设置键盘监听 //后面还会详细讲先有点印象 NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyBoardDidShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyBoardHiden(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil) } override func viewWillDisappear(_ animated: Bool) { //注销键盘监听 NotificationCenter.default.removeObserver(self,name: NSNotification.Name.UIKeyboardDidShow,object: nil) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardDidHide, object: nil) } //实现textfiled的委托协议方法 func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder(); NSLog("textfiled 获取焦点,点击return") return true; } //实现textView的委托协议方法 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if(text=="\n"){ textView.resignFirstResponder(); NSLog("textView获取焦点,点击return") } return true; } //设置监听函数 func keyBoardDidShow(notification:NSNotification){ NSLog("键盘显示!") } func keyBoardHiden(notification:NSNotification) { NSLog("键盘隐藏") } }
代码里面都有注释,不需要详细说了,先会大概使用就好,真正的熟练还是要靠真正的项目
3、开关控件、分段控件和滑块控件
开关控件UISwitch(相当于android的switch或者使用selector的checkButton)
分段控件UISegmentedControl(相当于android RadioGroup或者TabLayout)
滑块控件 UISlider (相当于android的seekBar)
下面的例子效果图:
直接看代码:
import UIKit class SwitchController: UIViewController { var rightSwitch: UISwitch! var leftSwitch: UISwitch! var segMents:UISegmentedControl! var sliderValue:UILabel! override func viewDidLoad() { super.viewDidLoad() let screen=UIScreen.main.bounds let switchScreenPace:CGFloat = 39 //初始化UISwitch self.leftSwitch=UISwitch() //设置在View视图中的位置,不用设置宽高 self.leftSwitch.frame.origin=CGPoint(x: switchScreenPace, y: 98) //设置switch是开启状态 self.leftSwitch.isOn=true self.leftSwitch.addTarget(self, action: #selector(SwitchController.valueChange(_:)), for:.valueChanged) self.view.addSubview(self.leftSwitch); self.rightSwitch=UISwitch() var frame=self.rightSwitch.frame frame.origin=CGPoint(x: screen.size.width-frame.size.width-switchScreenPace,y: 98) self.rightSwitch.frame=frame self.rightSwitch.isOn=true //设置开关事件 self.rightSwitch.addTarget(self, action: #selector(SwitchController.valueChange(_:)), for:.valueChanged) self.view.addSubview(self.rightSwitch) //初始化UISegmentedControl //设置分段控件菜单数量 let segments=["Left","Right"] self.segMents=UISegmentedControl(items:segments)//初始化菜单 let segWidth:CGFloat=220 let segHeight:CGFloat=29 self.segMents.frame=CGRect(x: (screen.size.width-segWidth)/2,y: 186,width: segWidth,height: segHeight) //设置菜单点击事件 self.segMents.addTarget(self, action:#selector(SwitchController.touchDow(sender:)), for:.valueChanged) self.view.addSubview(self.segMents) //初始化UILabel和UISlider let sliderValueTag:UILabel=UILabel.init(frame: CGRect(x: switchScreenPace,y: 297,width: 90, height: 31)) sliderValueTag.contentMode=UIViewContentMode.scaleAspectFill sliderValueTag.text="sliderValue:" self.view.addSubview(sliderValueTag) frame=sliderValueTag.frame self.sliderValue=UILabel.init(frame:CGRect(x: frame.origin.x+frame.width, y: 297 ,width: 90, height: 31)) self.view.addSubview(self.sliderValue) //初始化UISlider let slider=UISlider.init(frame: CGRect(x: switchScreenPace, y: 328, width: screen.width-2*switchScreenPace, height:31)) //设置slider的最小值和最大值,当前值 slider.minimumValue=0 slider.maximumValue=100 slider.value=27 //设置slider拖动时触发的事件 slider.addTarget(self, action: #selector(SwitchController.showSliderValue(sender:)), for:.valueChanged) self.sliderValue.text=String.init(format: "%d", Int(slider.value)) self.view.addSubview(slider) } func showSliderValue(sender:Any) { let slider = sender as! UISlider self.sliderValue.text=String(format:"%d",Int(slider.value)) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func valueChange(_ sender: Any) { let witchSwitch=sender as! UISwitch; let setting=witchSwitch.isOn; self.leftSwitch.setOn(setting, animated: true) self.rightSwitch.setOn(setting, animated: true) } func touchDow(sender:Any) { let segControl=sender as! UISegmentedControl //可以根据选中的菜单位置,做一些操作 NSLog("选中的segControl的序列%d",segControl.selectedSegmentIndex) //UISwitch是否隐藏,隐藏就显示,显示就隐藏 if(self.leftSwitch.isHidden==true){ self.leftSwitch.isHidden=false self.rightSwitch.isHidden=false }else{ self.leftSwitch.isHidden=true self.rightSwitch.isHidden=true } } }
4、Web视图:WKWebView类(相当于Android的WebView)
WKWebView是苹果ios8之后引入的新特性,用于替换旧的UIWebView,相对于之前更加的强大,我们来个简单的例子了解一下
效果图
import UIKit import WebKit //实现WKNavigationDelegate 主要是网络请求时的请求、加载、内容返回、加载失败等等 class ViewController: UIViewController,WKNavigationDelegate { var webView:WKWebView! override func viewDidLoad() { super.viewDidLoad() let frame=UIScreen.main.bounds //button之间的间距 let buttonSpace:CGFloat=10 //计算每个button的宽度 let buttonWidth:CGFloat=(frame.size.width-40)/3 //初始化button的容器,把下面的三个button放进去 let buttonBarView=UIView.init(frame: CGRect(x: buttonSpace,y: 20, width: frame.size.width-2*buttonSpace,height: 31 )) buttonBarView.backgroundColor=UIColor.blue self.view.addSubview(buttonBarView) //初始化三个UIButton 注意这里面的x和y的坐标是相对于buttonBarView的 let loadHtml=UIButton.init(type:UIButtonType.system) loadHtml.frame=CGRect(x: 0, y: 0,width: buttonWidth, height: 31) loadHtml.setTitle("loadHtml", for: UIControlState.normal) loadHtml.backgroundColor=UIColor.gray loadHtml.addTarget(self, action: #selector(ViewController.testLoadHtml(sender:)), for:.touchUpInside) let loadData=UIButton.init(type:UIButtonType.system) loadData.frame=CGRect(x: buttonWidth+buttonSpace, y: 0,width: buttonWidth, height: 31) loadData.setTitle("loadData", for: UIControlState.normal) loadData.backgroundColor=UIColor.darkGray let loadRequest=UIButton.init(type:UIButtonType.system) loadRequest.frame=CGRect(x: 2*buttonWidth+2*buttonSpace, y: 0,width: buttonWidth, height: 31) loadRequest.setTitle("loadRequest", for: UIControlState.normal) loadRequest.backgroundColor=UIColor.black loadRequest.addTarget(self, action: #selector(ViewController.testLoadRequest(sender:)), for:.touchUpInside) //把三个button添加到buttonBarView当中 buttonBarView.addSubview(loadHtml) buttonBarView.addSubview(loadData) buttonBarView.addSubview(loadRequest) //初始化WKWebView self.webView=WKWebView.init(frame: CGRect(x: buttonSpace,y :61, width: frame.size.width-2*buttonSpace, height: frame.size.height-61)) self.webView.backgroundColor=UIColor.red self.webView.navigationDelegate=self self.view.addSubview(self.webView) } //通不过也没关系,先了解一下,本地文件可能有问题 func testLoadHtml(sender:Any) { NSLog("testLoadHtml") let htmlPath=Bundle.main.path(forResource: "index", ofType: "html") let bundleUrl:URL=NSURL.fileURL(withPath: Bundle.main.bundleURL.absoluteString) do { let html = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue) self.webView.loadHTMLString(html as String, baseURL: bundleUrl) } catch let err as NSError { NSLog("loadHtml加载错误:%@", err.description) } } func testLoadRequest(sender:Any) { //注意这里使用的是http协议,苹果ios9之后要求必须使用https协议,但是有时候 //不得不适用http的时候 可以在info.plist里面添加App Transport Security Settings 键值,在添加 Allow Arbirary Loads 健,修改为YES let url=NSURL.init(string: "http://www.baidu.com") let request=NSURLRequest.init(url: url as! URL) self.webView.load(request as URLRequest) } //实现WKNavigationDelegate协议 //开始请求时调用 func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { NSLog("开始请求!") } //当内容开始返回时调用 func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { NSLog("内容返回!") } //加载完成后调用 func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { NSLog("加载完成!") } //加载失败 func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { NSLog("加载失败:%@", error.localizedDescription) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
5、警告框和操作表
警告框相当于Android的alertDialog
操作表 Android没有专门的控件,可以使用View或者popupWindow实现,也比较简单,不过貌似没有ios的封装的好
ios中警告框和操作表都是使用UIAlertController实现
咱们看下实际使用:
直接看代码吧都很简单,可以体会一下:
// // ViewController.swift // P4.6UIAlertController // // Created by LF on 2017/3/28. // Copyright © 2017年 yinwei. All rights reserved. // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let frame=UIScreen.main.bounds let buttonTop:CGFloat=77 let buttonSpace:CGFloat=33 //初始化按钮 let alertButton=UIButton.init(type: UIButtonType.system) alertButton.frame=CGRect(x: buttonSpace,y: buttonTop, width: frame.size.width-2*buttonSpace, height: 31) alertButton.setTitle("alert 警告框", for: UIControlState.normal) alertButton.addTarget(self, action: #selector(ViewController.testAlertDialog(sender:)), for: UIControlEvents.touchUpInside) self.view.addSubview(alertButton) let actionButton=UIButton.init(type: UIButtonType.system) actionButton.frame=CGRect(x: buttonSpace,y: 2*buttonTop+31,width: frame.size.width-2*buttonSpace,height: 31) actionButton.setTitle("sheet 操作表", for: UIControlState.normal) actionButton.addTarget(self, action: #selector(ViewController.testActionSheet(sender:)), for: UIControlEvents.touchUpInside) self.view.addSubview(actionButton) } func testAlertDialog(sender:Any) { //设置样式是警告框 let alertController:UIAlertController=UIAlertController.init(title: "alert", message: "警告框", preferredStyle: UIAlertControllerStyle.alert) let actionNo=UIAlertAction.init(title: "NO", style: UIAlertActionStyle.cancel){ (alertAction) -> Void in NSLog("no is selected!") } let actionYes=UIAlertAction.init(title: "YES", style: UIAlertActionStyle.default){ (alertAction) -> Void in NSLog("yes is selected") } //添加到controller中并且显示出来 alertController.addAction(actionNo) alertController.addAction(actionYes) self.present(alertController, animated: true, completion: nil) } func testActionSheet(sender:Any) { //默认构造函数就是操作表样式 let alertController:UIAlertController=UIAlertController() let actionA=UIAlertAction.init(title: "破坏性按钮", style: UIAlertActionStyle.destructive){ (alert) -> Void in NSLog("破坏性按钮,放到最上面不容易误碰") } let actionB=UIAlertAction.init(title: "微信", style: UIAlertActionStyle.default){ (alertAction) -> Void in NSLog("分享到微信去") } let actionCancel=UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.cancel){ (alertAction) -> Void in NSLog("隐藏操作表") } alertController.addAction(actionA) alertController.addAction(actionB) alertController.addAction(actionCancel) self.present(alertController, animated: true){ () -> Void in NSLog("操作表显示了出来了") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
6、等待相关的控件与进度条
android中的progressBar可以设置style样式,显示是圆形进度条或者是条形进度条,但是ios是两种方式实现的,接下来咱们看一下
// // ViewController.swift // P4.7WaitWidget // // Created by LF on 2017/3/28. // Copyright © 2017年 yinwei. All rights reserved. // import UIKit class ViewController: UIViewController { var activityIndiator:UIActivityIndicatorView! var progressView:UIProgressView! //定时器 模拟下载 var timer:Timer! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let screenFrame=UIScreen.main.bounds //距离顶部的距离 let topViewSpace:CGFloat=90 //按钮距离屏幕两边的距离 let buttonSpace:CGFloat=33 //条形进度条的起始位置 let progressSpace:CGFloat=17 //初始化圆形进度条 ,选择样式为 白色的 、大的 self.activityIndiator=UIActivityIndicatorView.init(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray) var activityFrame:CGRect=self.activityIndiator.frame activityFrame.origin=CGPoint(x: (screenFrame.size.width-activityFrame.size.width)/2,y:topViewSpace ) self.activityIndiator.frame=activityFrame self.activityIndiator.hidesWhenStopped=false self.view.addSubview(self.activityIndiator) //初始化一个显示上传的按钮 let uploadButton=UIButton.init(type: UIButtonType.system) uploadButton.frame=CGRect(x: buttonSpace,y: activityFrame.origin.y+50,width: screenFrame.size.width-2*buttonSpace,height: 31) uploadButton.setTitle("upload", for: UIControlState.normal) uploadButton.addTarget(self, action: #selector(ViewController.testActivityIndicator(sender:)), for: UIControlEvents.touchUpInside) self.view.addSubview(uploadButton) //初始化长形进度条 self.progressView=UIProgressView.init(frame: CGRect(x: progressSpace,y: uploadButton.frame.origin.y+50,width: screenFrame.size.width-2*progressSpace,height: 30)) self.view.addSubview(self.progressView) //初始化一个显示下载的按钮 let downButton=UIButton.init(type: UIButtonType.system) downButton.frame=CGRect(x: buttonSpace,y: self.progressView.frame.origin.y+30,width: screenFrame.size.width-2*buttonSpace,height: 31) downButton.setTitle("downLoad", for: UIControlState.normal) downButton.addTarget(self, action: #selector(ViewController.downLoadProgress(sender:)), for: UIControlEvents.touchUpInside) self.view.addSubview(downButton) } //显示圆形进度 或者隐藏 func testActivityIndicator(sender:Any) { if(self.activityIndiator.isAnimating){ self.activityIndiator.startAnimating() }else{ self.activityIndiator.startAnimating() } } //模拟下载程序 func downLoadProgress(sender:Any) { self.timer=Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.download), userInfo: nil, repeats: true) } func download() { self.progressView.progress+=0.1 if(self.progressView.progress==1.0){ self.timer.invalidate() let alertController:UIAlertController=UIAlertController.init(title: "下载提示", message: "下载完毕了", preferredStyle: UIAlertControllerStyle.alert) let action=UIAlertAction.init(title: "OK", style: UIAlertActionStyle.default){ (alert) -> Void in NSLog("知道了") } alertController.addAction(action) self.present(alertController, animated: true){ () -> Void in NSLog("弹出警示框!") } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
7、工具栏和导航栏
跟android中的toolbar比较像,废话不多说,直接上图,上代码
// // ViewController.swift // P4.4ToolBarAndNavigationBar // // Created by LF on 2017/3/23. // Copyright © 2017年 yinwei. All rights reserved. // import UIKit class ViewController: UIViewController { var label:UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let frame=UIScreen.main.bounds //初始化一个label self.label=UILabel.init(frame: CGRect(x: 0,y: frame.size.height/2, width: frame.size.width, height:31)) self.label.textAlignment=NSTextAlignment.center self.view.addSubview(self.label) //初始化工具栏 let toolBar=UIToolbar.init(frame: CGRect(x: 0,y: frame.size.height-44,width: frame.size.width, height: 44)) //使用系统按钮样式 例如:add 、save 等等最好使用系统自带按钮 let save=UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.save, target: self, action: #selector(ViewController.save(sender:))) //UIBarButtonItem 在工具栏和导航栏中是一样的 let open=UIBarButtonItem.init(title: "Open", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.open(sender:))) //初始化一个占位的按钮 let flexble=UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) toolBar.items=[save,flexble,open] self.view.addSubview(toolBar) //初始化导航栏 导航栏在每个界面都会出现 let navigationBar=UINavigationBar.init(frame: CGRect(x: 0,y: 20,width: frame.size.width,height: 44)) let saveNa=UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.save, target: self, action: #selector(ViewController.save(sender:))) let add=UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.add, target: self, action: #selector(ViewController.add(sender:))) let navigationItem=UINavigationItem.init(title: "") navigationItem.leftBarButtonItem=saveNa //左按钮 一般设置返回按钮呢 navigationItem.rightBarButtonItem=add navigationBar.items=[navigationItem]//设置导航栏的按钮 self.view.addSubview(navigationBar) } //sender的参数的意思是事件的发送者 func open(sender:Any) { self.label.text="点击了开启!" NSLog("点击了开启!") } func save(sender:Any) { self.label.text="点击了保存!" NSLog("点击了保存按钮!") } func add(sender:Any) { self.label.text="点击了添加!" NSLog("点击了添加!") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
8、UIImageView
相当于Android中的ImageView 但是功能强大一点貌似,好像ios中的所有跟Android相似的控件功能都要更强大,蛋疼
效果图:
直接上代码,很简单,会简单运用就行
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let frame=UIScreen.main.bounds let imageView=UIImageView.init(frame: CGRect(x: frame.size.width/2-100,y: frame.size.height/2 - 100,width: 200,height: 200)) imageView.contentMode=UIViewContentMode.scaleAspectFill imageView.image=UIImage.init(named: "girl") //设置圆角图片 这点可比android 方便多了 但是尽量不要使用比较占用 GPU // imageView.layer.masksToBounds=true // imageView.layer.cornerRadius=20 self.view.addSubview(imageView) }
接下来学习高级控件,加油!~
相关文章推荐
- IOS学习总结,基础控件+简单逻辑+简单功能+GCD。
- ios学习笔记(二)基础控件
- iOS学习笔记——基础控件(上)
- IOS学习之iOS开发UI基础—手写控件,frame,center和bounds属性
- iOS界面设计之基础控件的学习 --- UITextField
- ios学习第1章:实现控制器和视图-基础控件
- ios学习笔记(二)基础控件
- IOS学习之基础控件简介
- iOS基础控件的学习
- IOS学习四:基础控件Label,textfield,button等使用
- 黑马程序员——IOS学习四:基础控件Label,textfield,button等使用——黑马 ios 技术博客
- IOS 入门之基础控件学习1
- Asp.net控件开发学习笔记(三)-控件开发基础
- ios控件学习 代码实现
- [原][学习笔记][2011.11.14] ios的试图, 控件的总结.
- MFC基础,MFC自绘控件学习总结.
- 浅谈iOS MVC 学习基础
- 【android基础学习之二】——基础控件ListView
- ios控件学习2:
- 【android基础学习之五】——基础控件Menu,Dialog,ImageView,ImageButton