UIView 调整子视图的大小 [英] UIView resizes subview

查看:27
本文介绍了UIView 调整子视图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了奇怪的调整大小行为.

I am encountering a strange resizing behavior.

当我调整 UIView 的大小时,它的子视图的高度变为 0.

When I resize a UIView, it's subview's height goes to 0.

我想下面的代码只会调整视图的大小 - 而不是它的子视图.

I would imagine that the following code would only resize the view - not it's subview.

NSLog(@"view = %@", view);    
NSLog(@"subview = %@", subview);
CGRect frame = view.frame;
frame.size.height = 50;
view.frame = frame;
NSLog(@"view = %@", view);
NSLog(@"subview = %@", subview);

但是,上面产生了以下输出:

However, the above yields the following output:

view = <UIView: 0x97e68c0; frame = (0 0; 320 400); autoresize = RM+BM; layer = <CALayer: 0x97e5f10>>
subview = <UIView: 0xb87f510; frame = (0 0; 320 50); autoresize = W+H; layer = <CALayer: 0xb87f020>>
view = <UIView: 0x97e68c0; frame = (0 0; 320 50); autoresize = RM+BM; layer = <CALayer: 0x97e5f10>>
subview = <UIView: 0xb87f510; frame = (0 0; 320 0); autoresize = W+H; layer = <CALayer: 0xb87f020>>

有什么想法吗?

推荐答案

关键是日志输出中的这一段:

The key is this section in the log output:

autoresize = W+H;

这是UIView -autoresizingMask 的日志描述.检查文档 此处.基本上,这是您描述调整超视图大小时子视图应该做什么的方式.很强大.

This is the log description of the UIView -autoresizingMask. Check the documentation here. Basically, it's the way you describe what subviews should do when the superview is resized. It's very powerful.

您当前的规则告诉子视图在调整父视图大小时调整宽度和高度的大小,并保持子视图上方和下方的距离静态(就父视图坐标系而言).由于子视图不是很高,并且您正在大量调整父视图的大小,因此子视图会下降到零高度以尝试保持上下距离.

Your current rules tell the subview to resize width and height when the superview is resized, and to keep the distance above and below the subview static (in terms of the superview coordinate system). Because the subview isn't very high, and you're resizing the superview a lot, the subview drops to zero height to try to maintain the above and below distances.

这篇关于UIView 调整子视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆