我有一个小问题:是否可能以某种方式(没有故事板)在屏幕顶部创建一个小视图(如果有导航栏,那么在那之下),如果需要,它会显示错误/响应?< / p>
我没有在我创建的每个viewController上创建视图,只是通过代码?
或者您可以推荐一些扩展名吗?
例如:“没有互联网连接”
答案 0 :(得分:0)
您可以查看此答案 - https://stackoverflow.com/a/49129636/6080920
需要代码
extension UIViewController
{
func showNotificationView(message : String)
{
//base View
let baseView = UIView(frame: CGRect(x: 20, y: self.view.frame.size.height-(self.view.frame.size.height*0.15), width: self.view.frame.size.width-40, height: self.view.frame.size.height*0.08))
baseView.backgroundColor = UIColor.gray
baseView.clipsToBounds=true
self.view.addSubview(baseView)
//Image View
let imageView = UIImageView(image: UIImage(named: "RM_3"))
imageView.clipsToBounds=true
imageView.frame = CGRect(x: 0, y: 0, width: baseView.frame.size.width*0.2, height: baseView.frame.size.height)
baseView.addSubview(imageView)
//Label
let textLabel = UILabel(frame: CGRect(x: baseView.frame.size.width*0.2+10, y: 0, width: baseView.frame.size.width, height: baseView.frame.size.height))
textLabel.textColor = UIColor.white
textLabel.backgroundColor = UIColor.clear
textLabel.textAlignment = .left;
textLabel.numberOfLines = 0
textLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
textLabel.text = message
baseView.addSubview(textLabel)
}
}
<强>用法强>
@IBAction func navigate(_ sender: Any) {
self.showNotificationView(message: "hihihihh")
}