需求:原生UIAlertController文字都只能居中显示,在有些时候,我们需要提示文字左对齐的呀,就需要用代码来修改了
实现:定义
import UIKit
//用于UIAlertController的文字偏移
var subviews : [UIView]?
extension UIAlertController{
var TJL_titleLabel : UILabel?{
set{
}
get{
if TJL_viewArray(root: self.view) != nil {
return TJL_viewArray(root: self.view)![1] as? UILabel
}
return nil
}
}
var TJL_messageLabel : UILabel?{
set{
}
get{
if TJL_viewArray(root: self.view) != nil {
return TJL_viewArray(root: self.view)![2] as? UILabel
}
return nil
}
}
func TJL_viewArray(root:UIView) -> [UIView]?{
subviews = nil
for item in root.subviews{
if subviews != nil {
break
}
if item is UILabel{
subviews = root.subviews
return subviews!
}
TJL_viewArray(root: item)
}
return subviews
}
}
使用:
let alertVc = UIAlertController(title: "版本提示", message: "这是很长的提示哦\n这是第二句", preferredStyle: .alert)
alertVc.TJL_messageLabel?.textAlignment = .left 使用
// alertVc.TJL_titleLabel?.textAlignment = .right
let cancel = UIAlertAction(title: "稍后升级", style:.default) { (UIAlertAction) in
}
let done = UIAlertAction(title: "升级", style:.default) { (UIAlertAction) in
}
alertVc.addAction(cancel)
alertVc.addAction(done)
self.present(alertVc, animated: false, completion: nil)
效果:

网友评论