iOS 管理View
创建: 2018/04/26
完成: 2018/05/03
更新: 2018/05/04 增加UIStackView
更新: 2018/09/18 补充SFSafariViewController需要SafariServeces
视图管理器 | |||||||||||||||||||||
一个画面一个视图管理器, 管理View | |||||||||||||||||||||
画面迁移 | |||||||||||||||||||||
segue的transition |
|
||||||||||||||||||||
视图管理器的生命周期 | |||||||||||||||||||||
View-Related Notifications |
也叫生命周期方法
顺序: viewDidLoad -> viewWillAppear -> viewWillLayoutSubViews -> viewDidLayoutSubViews -> viewDidAppear -> viewWillDisappear -> viewDidDisappear func lifeCycleDebugTest(string: String) { print("now in \(string)") } // 生命周期测试 override func viewDidLoad() { super.viewDidLoad() lifeCycleDebugTest(string: #function) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) lifeCycleDebugTest(string: #function) } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) lifeCycleDebugTest(string: #function) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) lifeCycleDebugTest(string: #function) } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) lifeCycleDebugTest(string: #function) } override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() lifeCycleDebugTest(string: #function) } override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() lifeCycleDebugTest(string: #function) } override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { super.viewWillTransition(to: size, with: coordinator) lifeCycleDebugTest(string: #function) }
|
||||||||||||||||||||
状态栏 | |||||||||||||||||||||
最上方, 显示时间电量等 | |||||||||||||||||||||
样式 |
preferredStatusBarStyle //------------------------------------------------------- // 状态栏 //------------------------------------------------------- var style = UIStatusBarStyle.default override var preferredStatusBarStyle: UIStatusBarStyle { return style } @IBAction func changeStatusBarStyle(_ sender: UIButton) { style = ((style == UIStatusBarStyle.default) ? .lightContent : .default) setNeedsStatusBarAppearanceUpdate() }
|
||||||||||||||||||||
是否隐藏 |
prefersStatusBarHidden // 切换显示 prefersStatusBarHidden var isHidden = false override var prefersStatusBarHidden: Bool { return isHidden } @IBAction func toggleStatusBar(_ sender: UIButton) { isHidden = !isHidden setNeedsStatusBarAppearanceUpdate() }
|
||||||||||||||||||||
app全局设定 | info -> Custom iOS Target Properties -> 增加View controller based status bar appearance -> Value设为NO | ||||||||||||||||||||
方向与旋转 | |||||||||||||||||||||
supportedInterfaceOrientations |
值
|
||||||||||||||||||||
代码设定 |
重写supportedInterfaceOrientations override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return [.landscapeLeft] // 只支持向左横倾 }
|
||||||||||||||||||||
全局设定 | General -> Deployment Info -> Device Orientation | ||||||||||||||||||||
导航控制器(navigation controller) | |||||||||||||||||||||
导航, 上方有导航栏(navigation bar), 自带返回按钮 | |||||||||||||||||||||
把已有viewController 插入导航控制器 |
选中目标视图控制器 -> Editor -> Embed in -> Navigation Controller | ||||||||||||||||||||
使用 | 按钮等往其他视图控制器走, 自动添加返回按钮 | ||||||||||||||||||||
代码segue移动 |
navigationController?.pushViewController
|
||||||||||||||||||||
tab bar controller | |||||||||||||||||||||
直接用 | |||||||||||||||||||||
简易使用table view | |||||||||||||||||||||
自带UITableViewDataSource, UITableViewDelegate | |||||||||||||||||||||
增加编辑功能 |
● 实现编辑按钮 (非必须) (1)先套进navigation controller (2)绑定编辑按钮 位置: viewDidLoad self.navigationItem.rightBarButtonItem = self.editButtonItem ● 实现功能 override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { // 是否可编辑 // Return false if you do not want the specified item to be editable. return true } override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle,
|
||||||||||||||||||||
简易使用collection view | |||||||||||||||||||||
和CollectionView差不多 | |||||||||||||||||||||
管理page UIPageViewController | |||||||||||||||||||||
● 实现协议 extension PageViewController: UIPageViewControllerDataSource { // 设置当前页面的前一页 func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { if let index = self.targetViewControllers.index(of: viewController), index > 0 { return self.targetViewControllers[index - 1] } return nil } // 设置当前页面的后一页 func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { if let index = self.targetViewControllers.index(of: viewController), index < self.targetViewControllers.count - 1 { return self.targetViewControllers[index + 1] } return nil } }
● 创建并展示 //ViewDidLoad override func viewDidLoad() { // 设置dataSource, delegate self.dataSource = self self.delegate = self super.viewDidLoad() // 设置pages self.targetViewControllers = [] for id in self.identifierIDs { self.targetViewControllers.append((self.storyboard?.instantiateViewController(withIdentifier: id))!) } // 设置一开始显示的viewController setViewControllers([targetViewControllers[0]], direction: .forward, animated: true) }
|
|||||||||||||||||||||
alert | |||||||||||||||||||||
@IBAction func showSampleAlert(_ sender: UIButton) { // 创建UIAlertController let alertController = UIAlertController.init(title: "改变背景颜色", message: "真的要改变吗", preferredStyle: .alert) // 创建取消按钮 let cancelAlertButton = UIAlertAction.init(title: "取消", style: .cancel, handler: nil) // 创建确定按钮 let okAlertButton = UIAlertAction.init(title: "确定", style: .default, handler: { action in self.view.backgroundColor = UIColor.red }) // 添加到alert里 alertController.addAction(okAlertButton) alertController.addAction(cancelAlertButton) // 显示alert present(alertController, animated: true, completion: nil) }
|
|||||||||||||||||||||
action sheet | |||||||||||||||||||||
@IBAction func showSampleActionSheet(_ sender: UIButton) { // 创建UIAlertController let alertController = UIAlertController.init(title: "动作菜单", message: "随便选一个啊", preferredStyle: .actionSheet) // 创建选项1, 2, 3 let firstAction = UIAlertAction.init(title: "选项1", style: .default, handler: { action in self.actionSheetMirror.text = "选了选项1" }) let secondAction = UIAlertAction.init(title: "选项2", style: .destructive) { (action) in self.actionSheetMirror.text = "选了选项2" } let thirdAction = UIAlertAction.init(title: "选项3", style: .cancel) { (action) in self.actionSheetMirror.text = "选了选项3" } // 添加到action sheet alertController.addAction(firstAction) alertController.addAction(secondAction) alertController.addAction(thirdAction) // 显示action sheet present(alertController, animated: true) { print("ok") } }
|
|||||||||||||||||||||
选择图象(UIImagePickerController) | |||||||||||||||||||||
● 需要允许接入相册 设定请求权限时表示的信息 [Info] -> [Custom iOS Target Properties] -> 添加 [Privacy - Photo Library Usage Description] -> 在值处设置请求权限时表示的信息 ● 扩张 extension ImagePickerControllerViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { self.imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage dismiss(animated: true, completion: nil) } }
● 按钮的处理 @IBAction func chooseImage(_ sender: UIButton) { // 选择图象 let picker = UIImagePickerController.init() picker.delegate = self picker.sourceType = .photoLibrary present(picker, animated: true, completion: nil) }
|
|||||||||||||||||||||
用别的应用来打开(UIActivityViewController) | |||||||||||||||||||||
@IBAction func shareSelf(_ sender: UIButton) { let activityController = UIActivityViewController.init( activityItems: ["分享"], applicationActivities: nil ) present(activityController, animated: true, completion: nil) }
|
|||||||||||||||||||||
显示网页(SFSafariWebViewController) | |||||||||||||||||||||
需要的头文件 |
import SafariServeces
|
||||||||||||||||||||
@IBAction func showWebPage(_ sender: UIButton) { // SFSafariWebViewController guard let url = URL.init(string: "https://github.com/") else { return } let safariWebViewController = SFSafariViewController.init(url: url) present(safariWebViewController, animated: true, completion: nil) }
|
|||||||||||||||||||||
UIStackView | |||||||||||||||||||||
排列子View | |||||||||||||||||||||