【问题标题】:How to set UIImageView frame in ViewController for Universal App?如何在 ViewController 中为 Universal App 设置 UIImageView 框架?
【发布时间】:2012-12-09 19:34:19
【问题描述】:

对于通用应用,如何在 ViewController 中设置 UIImageView 框架?

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

[self.view addSubview:baseView];

// Displays UIImageView

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

当我运行应用程序时,它显示黑屏。所以我想我必须为它设置一个框架。

【问题讨论】:

  • 什么意思如何设置框架?只需设置它。但是您的意思是如何考虑不同的分辨率?像 CGRect screenBounds = [[UIScreen mainScreen] bounds]; 这样获取屏幕尺寸,并使用这些值来正确定位和调整您的视图。
  • 感谢您的帮助。欣赏它。

标签: iphone objective-c ios uiimageview ios6


【解决方案1】:

您有点回答了自己的问题 - 是的,您必须为它设置一个框架。 你可以试试:

UIView *baseView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:baseView];

// Displays UIImageView

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

您还可以为 baseView 和 myImage 设置 autoresizingmask,以便当 self.view 的框架发生变化时,这两个视图也会发生变化。

此外,以防万一您不使用 ARC - 不要忘记在离开此方法之前自动释放这些视图。

【讨论】:

  • 感谢您的帮助。欣赏它。
猜你喜欢
  • 1970-01-01
  • 2013-10-08
  • 2023-03-19
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
相关资源
最近更新 更多