ios 如何以编程方式设置 UIView 大小以匹配父级而不受约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37725406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set UIView size to match parent without constraints programmatically
提问by Seifolahi
The problem sounds easy but it is making me crazy. I've created a white view in IB that's called iBag and by constraints it's size depends on screen size.
这个问题听起来很简单,但它让我发疯。我在 IB 中创建了一个名为 iBag 的白色视图,它的大小取决于屏幕大小。
Now I want create a new UIView programmatically and add as subview to iBag with same size and position by this code
现在我想以编程方式创建一个新的 UIView 并通过此代码添加为具有相同大小和位置的 iBag 的子视图
let newView = UIView()
newView.frame = (frame: CGRect(x: 0, y: 0, width: iBag.frame.width, height: iBag.frame.height))
newView.backgroundColor = UIColor.redColor()
iBag.addSubview(newView)
I also tried bounds but that didn't help. I can use constraints to solve the problem but i want to understand what's wrong.
我也尝试过边界,但这没有帮助。我可以使用约束来解决问题,但我想了解出了什么问题。
回答by Vic
Try this:
尝试这个:
Swift 1 and 2:
斯威夫特 1 和 2:
newView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
Swift 3+:
斯威夫特 3+:
newView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
If it doesn't work, also this:
如果它不起作用,还有这个:
iBag.autoresizesSubviews = true
iBag.autoresizesSubviews = true
回答by Shadow Of
So many answers and nobody is explaining what's wrong.
这么多答案,没有人解释什么是错的。
I will try.
我会尝试。
You are setting the frame of newView
to your superviews frame beforethe autolayout engine has started to determine your superviews position and size. So, when you use the superviews
frame, you are using its initial frame. Which is not correct in most cases.
newView
在自动布局引擎开始确定您的超级视图位置和大小之前,您正在将框架设置为您的超级视图框架。因此,当您使用superviews
框架时,您正在使用其初始框架。在大多数情况下这是不正确的。
You have 3 ways to do it correctly:
您有 3 种方法可以正确执行此操作:
Use autolayout constraints for your
newView
Set
newView
s frame in theviewDidLayoutSubviews
method. Which is called when the autolayout engine finishes determining the frames actual values. (Note: This method can be called multiple times)- Set an autoresizing mask for
newView
使用自动布局约束为您
newView
newView
在viewDidLayoutSubviews
方法中设置s帧。当自动布局引擎完成确定帧实际值时调用。(注:此方法可多次调用)- 设置自动调整大小掩码
newView
回答by Cjay
Try this
尝试这个
subview.frame = parentView.bounds
subview.frame = parentView.bounds
subview.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
subview.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
parentView.addSubview(subview)
parentView.addSubview(subview)
回答by Ketan Parmar
Try this,
尝试这个,
override func viewDidLayoutSubviews() {
newView.frame = (frame: CGRect(x: 0, y: 0, width: iBag.frame.width, height: iBag.frame.height))
}
or you can use viewWillAppear
or viewDidAppear
to set frame.
或者您可以使用viewWillAppear
或viewDidAppear
来设置框架。
Because in viewDidload
your iBag's frame is same which is in your interface builder (storyboard). Now it will change according to your constraints and device size on which you run. So, in viewDidload
your new view get old frame not changed frame. So, it's better to use viewDidLayoutSubviews
or viewWilAppear
.
因为在viewDidload
您的 iBag 的框架中与您的界面构建器(故事板)中的框架相同。现在它将根据您的约束和运行的设备大小而改变。因此,在viewDidload
您的新视图中获取旧框架而不是更改框架。所以,最好使用viewDidLayoutSubviews
or viewWilAppear
。
Second thing, Either use autolayout everywhere or nowhere. It is bad practice to use autolayout in parent view and not to child.
第二件事,要么在任何地方使用自动布局,要么在任何地方都不使用。在父视图而不是子视图中使用自动布局是不好的做法。
回答by Nirav D
May be this will help you
可能这会帮助你
override func viewDidLayoutSubviews() {
let newView = UIView()
newView.frame = iBag.bounds
newView.addConstraints(iBag.constraints)
newView.backgroundColor = UIColor.redColor()
iBag.addSubview(newView)
}
Hope this will help you.
希望这会帮助你。
回答by Ccr
may be this will work:-
可能这会起作用:-
subview.bounds.size = parentView.bounds.size
subview.center = parentView.center
but in your case, width
and height
inside the CGRect should be
但在你的情况,width
并height
在里面的CGRect应
width: parentView.bounds.size.width
height: parentView.bounds.size.height
and then add to it's parent as subview.
然后作为子视图添加到它的父级。
回答by Bhadresh Kathiriya
try this code:
试试这个代码:
let newView = UIView()
newView.frame = (frame: CGRect(x: iBag.frame.origin.x, y: iBag.frame.origin.y, width: iBag.frame.size.width, height: iBag.frame.size.height))
newView.backgroundColor = UIColor.redColor()
iBag.addSubview(newView)
回答by shubh14896
If you are setting up your view in viewdidload, call it in viewdidappear, so it captures the original frame of view, accordingly to the screen size
如果您在 viewdidload 中设置视图,请在 viewdidappear 中调用它,因此它会根据屏幕大小捕获原始视图帧
回答by Duncan C
Not using constraints is likely what's wrong. If your storyboard/nib is set to use AutoLayout (which is on by default) then setting frames/bounds gets overridden by the AutoLayout system and you HAVE TO use constraints to get your layout to look right. (or set the flag that converts auto resizing masks to constraints. I don't remember what that flag is called and can't seem to find it at the moment.)
不使用约束很可能是错误的。如果您的故事板/笔尖设置为使用 AutoLayout(默认情况下处于启用状态),则设置框架/边界会被 AutoLayout 系统覆盖,您必须使用约束才能使您的布局看起来正确。(或设置将自动调整大小掩码转换为约束的标志。我不记得那个标志叫什么,目前似乎找不到它。)