简体   繁体   English

UIImageView无法正确更新边界

[英]UIImageView not correctly updating bounds

I am currently trying to program a game with a HP gauge represented by an UIImageView placed on a .xib file. 我目前正在尝试使用放置在.xib文件上的UIImageView表示的HP仪表对游戏进行编程。 I declared my IBOutlet as follows: 我声明了IBOutlet如下:

@property (weak, nonatomic) IBOutlet UIImageView *hpGaugeView;

and synthesized accordingly. 并据此进行综合。 It is also linked to the UIImageView instance in the xib. 它还链接到xib中的UIImageView实例。

In the xib, I set the length of hpGaugeView to 188 and height to 9. 在xib中,我将hpGaugeView的长度设置为188,将高度设置为9。

Now this is what I did in viewDidLoad: 现在,这就是我在viewDidLoad中所做的:

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

    [hpGaugeView setFrame:CGRectMake(49, 18, 123, 9)];
    NSLog(@"%f, %f", [hpGaugeView frame].size.width, [hpGaugeView frame].size.height);
}

Although NSLog tells me the hpGaugeView's width is 123, when I run it the length appears unchanged compared to the one in xib. 尽管NSLog告诉我hpGaugeView的宽度为123,但运行它时,其长度与xib中的宽度相比没有变化。 All the other setFrame: or setBound: worked fine in other viewControllers, so I'm just wondering what's happening here. 所有其他setFrame:或setBound:在其他viewController中都可以正常工作,所以我只是想知道这里发生了什么。

It is a small but crucial problem that is bugging me for a few hours straight... 这是一个很小但很关键的问题,连续几个小时困扰着我……

Edit: When I turn off AutoLayout, the resizing worked; 编辑:关闭自动版式时,调整大小有效; it creates a bunch of other problems though... 它会带来很多其他问题...

With autolayout on, the subviews have not been laid during the viewDidLoad. 启用自动布局后,尚未在viewDidLoad期间放置子视图。 The constraints are calculated a bit later and you can override the viewDidLayoutSubviews method to set your frame: 稍后会计算约束,您可以重写viewDidLayoutSubviews方法来设置框架:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    // Do any additional setup after subviews are laid.
    [hpGaugeView setFrame:CGRectMake(49, 18, 123, 9)];
    NSLog(@"%f, %f", [hpGaugeView frame].size.width, [hpGaugeView frame].size.height);
}

From the class reference: 从类参考:

viewDidLayoutSubviews viewDidLayoutSubviews

Notifies the view controller that its view just laid out its subviews. 通知视图控制器其视图刚刚布置了子视图。

I believe the problem is that you're not changing the frame of the UIImageView . 我相信问题是您没有更改UIImageView的框架。 The bounds refer to the position and size of the view in it's own coordinate system whereas the frame refers to the position and size in it's superview coordinate system. 边界是指视图在其自己的坐标系中的位置和大小,而框架是指视图在其超级视图坐标系中的位置和大小。 If the clipsToBounds property is set yo YES, then nothing outside the frame of your view will be visible. 如果将clipsToBounds属性设置为YES,则视图框架之外的任何内容都将不可见。

事实证明,笔尖和代码之间存在一些怪异的怪异相互作用……当我以编程方式创建UIImageView时,它运行良好。

AutoLayout property worked only above IOS 6.0 only . AutoLayout属性仅在IOS 6.0以上版本中有效。 Dragging of control to XIB instead of you can create the UIImageView through Code and add to current view. 将控件拖到XIB而不是您可以通过Code创建UIImageView并添加到当前视图。 I have given the code for adding UIImageView programmatically. 我已经给出了用于以编程方式添加UIImageView的代码。

Source Code: 源代码:

// .h file
@property (weak, nonatomic) UIImageView *hpGaugeView;

//.m file
- (void)viewDidLoad
{
    [super viewDidLoad];

    hpGaugeView = [[UIImageView alloc] init];

    [hpGaugeView setFrame:CGRectMake(49, 18, 123, 9)];

    [self.view addSubview:hpGaugeView];

}

Initialize and add the hpGaugeView to subview inside the viewDidLoad function through programmatically. 通过编程初始化hpGaugeView并将其添加到viewDidLoad函数内的子视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM