【问题标题】:How to set an image as background directly on a view without using imageview?如何在不使用 imageview 的情况下直接在视图上将图像设置为背景?
【发布时间】:2017-07-20 12:51:47
【问题描述】:
 UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.jpg"]];
    _mainView.backgroundColor = background;

当我运行此代码时,我将图像作为视图的背景,但喜欢几种模式。我想在不使用 ImageView 的情况下将图像设置为我的视图。 可以通过 stroyboard 做吗?

【问题讨论】:

标签: ios objective-c uiview uiimageview xcode-storyboard


【解决方案1】:

您可以使用 CaLayer 对象,将您的图像指定为视图层的内容。 其他项目可以像以前一样添加为视图的子视图。

class CustomViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // assign image as content of view's layer
        view.layer.contents = UIImage(named: "yourImageName")!.cgImage
    }   
} 

更多信息:

Apple's CALayer doc

CALayer tutorial

【讨论】:

    【解决方案2】:

    当我运行此代码时,我将图像作为视图的背景,但是 喜欢几种模式。

    因为您创建了一个模式并将其设置为UIView。方法initWithPatternImage 使用指定的 Quartz 颜色参考初始化并返回一个颜色对象。

    最好你可以使用UIImageView并将其添加到UIView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      相关资源
      最近更新 更多