- - .
UIView, UILabel.
:
private var bannerLabel: UILabel = UILabel()
private let view = UIView()
let greenColor = UIColorFromRGB(63,g: 161,b: 81)
let warningColor = UIColorFromRGB(252,g: 140,b: 38)
let errorColor = UIColorFromRGB(252,g: 66,b: 54)
- .
func showBannerWithOption(title: String, color: UIColor) {
view.frame = CGRect(x: 50, y: 150, width: 200, height: 45)
let window = UIApplication.shared.keyWindow
guard let mainWindow = window else { return }
mainWindow.addSubview(view)
view.centerXAnchor.constraint(equalTo: mainWindow.centerXAnchor).isActive = true
view.centerYAnchor.constraint(equalTo: mainWindow.centerYAnchor).isActive = true
bannerLabel.frame = CGRect(x: 50, y: 150, width: 200, height: 21)
bannerLabel.backgroundColor = color
bannerLabel.textColor = UIColor.black
bannerLabel.textAlignment = .center
bannerLabel.text = title
view.addSubview(bannerLabel)
bannerLabel.translatesAutoresizingMaskIntoConstraints = false
bannerLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
bannerLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
bannerLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
bannerLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
}
: CGRect, (). .
.
The showBannerWithOption function with the name and color is as follows if you need it in more than one place.
if you want to call this function just do this:
override func viewDidLoad() {
super.viewDidLoad()
showBannerWithOption(title: "test", color: greenColor)
}
That's all :) It's quick 5.0
Now you can customize how you want.
source
share