相关疑难解决方法(0)

在CALayer中添加UIImage

我必须添加一个UIImageView作为MapView的子视图.为此,我在MapView上创建了一个图层.在这一层,我想放置我的图像,但我得到一个白色的矩形,没有别的.我的图片不可见.

这是代码:

- (void)viewDidLoad
{
    //......

    CALayer *layer = [CALayer layer];
    layer.backgroundColor = [[UIColor whiteColor] CGColor];

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        layer.bounds = CGRectMake(self.mapView.bounds.origin.x,
                                  self.mapView.bounds.origin.y, 80, 300);
    }
    else
    {
        layer.bounds = CGRectMake(self.mapView.frame.origin.x,
                                  self.mapView.frame.origin.y, 150, 700);
    }

    layer.contents = (id)[UIImage imageNamed:@"myImage.png"];
    //the name is correct but  in the output the image is not visible

    [[self.mapView layer] addSublayer:layer];
    [layer setNeedsDisplay];
}
Run Code Online (Sandbox Code Playgroud)

subview calayer uiimageview uiimage ios

40
推荐指数
3
解决办法
4万
查看次数

UIViewContentMode模式指的是什么类型的内容?

根据UIView关于该contentMode物业的官方文件:

The content mode specifies how the cached bitmap of the view’s layer is adjusted when the view’s bounds change
Run Code Online (Sandbox Code Playgroud)

什么定义了这个定义中的内容?它是子视图还是我们为视图定义背景颜色时的示例.

我的第一个猜测是它应该至少应用于视图中的子视图,但是例如下面的代码片段在使用UIViewContentModeCenter标记时不会给我预期的结果 :

 UIView* redView = [[UIView alloc] initWithFrame:CGRectMake(80, 80, 150, 200)];
 redView.contentMode = UIViewContentModeCenter;
 redView.backgroundColor = [UIColor redColor];

 UIView* greenView = [[UIView alloc] initWithFrame:redView.bounds];
 greenView.backgroundColor = [UIColor greenColor];
 [redView addSubview:greenView];

 redView.frame = CGRectInset(redView.frame, -5, -5);
 [self.view addSubview:redView];
Run Code Online (Sandbox Code Playgroud)

我刚刚设置了一个包含greenView的redView.我还设置了redview的内容模式UIViewContentModeCenter- 为什么在我编写的代码中,当我更改其父级的框架时,greenView不居中?不应UIViewContentModeCenter该做什么?

谢谢你的澄清!

Ps:您可以在loadView简单的视图控制器模板项目中轻松测试上述代码.

uiview contentmode ios5 uiview-hierarchy

8
推荐指数
2
解决办法
8424
查看次数