12.swift5-常用UI组件

UIStoryboard

//1.通过stroyboard获取控制器

let childVc = UIStoryboard(name: "Home", bundle: nil).instantiateInitialViewController()!

顶部导航栏UIBarButtonItem

controller继承UIViewController,子类有navigationItem(UINavigationItem)

let btn = UIButton()

btn.setImage(UIImage(named: "logo"), for: UIControl.State.normal)//logo是图片资源名

//swift的图片资源不用写路径名,要求图片资源唯一

顶部导航栏左边按钮:navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)

顶部导航栏右边按钮数组navigationItem.rightBarButtonItems = []

UITapGestureRecognizer:

假如你想要在你的应用中检测手势,例如点击,缩放,平移,或者旋转,用Swift和内建的UIGestureRecognizer类实现是非常容易的

let tapGes = UITapGestureRecognizer(target: self, action: #selector(self.titleLabelClick(tagGes:)))

label.addGestureRecognizer(tapGes)

//获取手势所在view

@objc private func titleLabelClick(tagGes: UITapGestureRecognizer){

guard let currentLabel = tagGes.view as? UILabel else {return}

}

//获取位置

func picTap(sender: UITapGestureRecognizer) {        

        let point = sender.location(in: sender.view)

}

alert对话框

注意,最后要present才会弹出对话框,然后是一个alertController里面加alertAction,可以有多个alertAction

let alertController = UIAlertController(title:"提示", message:"学生账号注册暂未开放z", preferredStyle: .alert)

let alertOK = UIAlertAction(title: "好的", style: .cancel, handler:nil)

alertController.addAction(alertOK)

present(alertController, animated:true, completion: nil)

UITextField

UITextField加placeholder:

textField.attributedPlaceholder = NSAttributedString.init(string:"请输入用户名", attributes: [

            NSAttributedString.Key.foregroundColor:UIColor.lightGray])

你可能感兴趣的:(12.swift5-常用UI组件)