UIImageView Bounds,Frame

时间:2014-02-16 08:34:23

标签: ios objective-c uiimageview frame bounds

我知道这里有很多类似的问题,我检查了一个着名的问题,然后抓住了Bounds和Frame之间的区别。

现在我遇到了一些与他们有关的问题。我和他们一起玩,但它并没有像我预期的那样显示出来。

我在这里不明白的是:

  • 为什么Y的帧起点低于顶部44.000000,即使我在左下角设置了UIImageView? 因为边界应该是 “UIView的边界是矩形,表示为相对于其自身坐标系(0,0)的位置(x,y)和大小(宽度,高度)。” (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);







}

2 个答案:

答案 0 :(得分:1)

44来自导航栏,占据了那么高的高度

答案 1 :(得分:1)

44幻数值实际上来自导航栏:)而状态栏的高度为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)]

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