ios - UIImageView 边界、框架

标签 ios objective-c uiimageview frame bounds

我知道这里有很多类似的问题,我查看了著名的问题,然后掌握了 Bounds 和 Frame 之间的区别。

现在我有一些与他们相关的问题。我和他们一起玩,但没有像我预期的那样显示。

这里我不明白的是:

  • 即使我将 UIImageView 设置在左角,为什么 Y 的框架原点在顶部下方 44.000000? 因为边界应该是 “UIView 的边界是矩形,表示为相对于其自身坐标系 (0,0) 的位置 (x,y) 和大小 (width,height)。” ( Cocoa: What's the difference between the frame and the bounds? ) 我认为框架也应该从这里的左上角开始。

enter image description here

#import "ViewController.h"

@interface ViewController ()

//@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UIImageView *image2;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


//UIImage *imageView = [UIImage imageNamed:@"stanford"];

//    self.image.alpha = 1;
//    self.image.contentMode = 1;
//    self.image.image = imageView;
//    [self.image setAlpha:0.1];
//    [self.image setContentMode:UIViewContentModeCenter];
//    [self.image setImage:imageView];



    NSURL *url = [[NSURL alloc] initWithString:@"http://upload.wikimedia.org/wikipedia/commons/thumb/e/ed/Pitbull_2%2C_2012.jpg/472px-Pitbull_2%2C_2012.jpg"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *imageData = [UIImage imageWithData:data];

    [self.image2 setBounds:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.image2 setImage:imageData];

    NSLog(@"frame.orign.x:%f,frame.origin.y:%f",self.image2.frame.origin.x,self.image2.frame.origin.y);







}

最佳答案

44 魔数(Magic Number)值实际上来自导航栏 :) 而状态栏的高度是 20 点。

如果您希望您的图像覆盖整个屏幕,那么您需要去掉状态栏,或者使您的状态栏半透明,以便内容可以显示在下方。

如果您没有将状态栏/导航栏设置为半透明,则原点 0,0 会像您现在看到的那样从栏下方开始。

状态栏使用

[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackTranslucent];

导航栏,如果你有一个显示可以使用这个设置

theNavigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

只有这样,您的以下代码才会全屏显示您的内容

[self.image2 setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]

这是因为将条形设置为半透明会使它们显示为叠加层。

关于ios - UIImageView 边界、框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21808960/

相关文章:

ios - 迁移到 Xcode 8 和 Swift 3.0 时 Xcode Healthkit 授权函数错误

ios - 比较来自不同日历的两个日期组件

c# - 如何从 DLL 设置委托(delegate)/回调(C 和 C# 之间的互操作)

ios - 来自 iPhone 设置和 GMT 偏移的日期时间格式

ios - 在 iOS 中使用 soap 在 https websrevice 中发送和接收数据 - Obj-C

ios - objc_setAssociatedObject 保留原子或非原子

ios - 我可以在 AppDelegate.m 中注销 NSNotificationCenter 吗?

ios - 如何在不旋转的情况下在 ImageView 中适合大图像?

Xcode Imageview Aspect Fit 删除多余空间

ios - UIImageView 加载较大图像后调整大小