我有以下UIView擴展來添加背景。Swift /代碼重構/如何將參數添加到UIView
extension UIView {
func addBackground() {
// screen width and height:
let width = UIScreen.mainScreen().bounds.size.width
let height = UIScreen.mainScreen().bounds.size.height
let imageViewBackground = UIImageView(frame: CGRectMake(0, 0, width, height))
imageViewBackground.image = UIImage(named: "index_clear")
imageViewBackground.clipsToBounds = true
// you can change the content mode:
imageViewBackground.contentMode = UIViewContentMode.ScaleAspectFill
self.addSubview(imageViewBackground)
self.sendSubviewToBack(imageViewBackground)
}}
我
self.view.addBackground()
什麼使通用的擴展的最佳實踐打電話了嗎?我想改變這樣的畫面:
self.view.addBackground("index_clear")
或
self.view.addBackground("other_background_image")
幫助是非常讚賞。
有關添加什麼參數的方法? 'func addBackground(imageName:String)' –
請注意,這與編程語言中「generic」的一般含義無關。這更多的是介紹一個參數 - 這是一個非常基本的任務 - 你真正的問題是什麼?只需爲該函數添加一個參數即可。 – luk2302
我改變了標題。感謝您的關注。 –