UIview定义背景图片

时间:2023-03-08 22:19:31

UIImage *image = [UIImage imageNamed:@"bgimagename"];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
    //view.backgroundColor = [UIColor colorWithPatternImage:image]; //这种是正常的图片转成背景色
    view.layer.contents = (id)image.CGImage;//这种的比较好,可支持半透明,兼容性又好。
    [self.view addSubview:view];