开发者社区> 问答> 正文

在 ViewController设置UIImageView框架

对于一般的程序,怎么样在 ViewController设置UIImageView框架?

UIView *baseView = [[UIView alloc] init];

[self.view addSubview:baseView];

// 显示UIImageView

UIImageView *myImage = [[UIImageView alloc]
                        initWithImage:[UIImage imageNamed:@"tool.png"]];
[baseView addSubview:myImage];

运行之后就黑屏,我猜是需要设置框架。

展开
收起
爵霸 2016-05-27 10:36:11 1788 0
1 条回答
写回答
取消 提交回答
  • 你说对了,是需要框架,你可以试试下面的:

    UIView *baseView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:baseView];
    
    // 显示UIImageView
    
    UIImageView *myImage = [[UIImageView alloc]
                        initWithImage:[UIImage imageNamed:@"tool.png"]];
    myImage.frame = baseView.bounds;
    [baseView addSubview:myImage];

    同时还有对baseView和myImage设置autoresizingmask`,这样当视图的框架改变时,这两个视图也自动调整。

    如果没用ARC,记得要在方法结束之前自动释放这些视图

    2019-07-17 19:17:15
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载