how to set a background image as colorWithPatternImage in swift

后端 未结 7 703
心在旅途
心在旅途 2020-12-23 11:10

My questions is, is it possible to have a UIView display an image, and if so, how so I do it? All I can find is the colorwithpatternImage which no longer works

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-23 12:00

    Refer to the UIColor documentation.

    In Swift, you have to call a convenience initializer. This is because in Swift, all Objective-C class methods which return an instance of their class become convenience initializers.

    Here's how it looks in Swift:

     self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))
    

    + (UIColor *)colorWithPatternImage:(UIImage *)image returns a UIColor instance, so it will become a convenience initializer in Swift. Similarly, UIImage imageNamed: becomes init(patternImage image: UIImage!).

提交回复
热议问题