objective-c 使用 addSubview 添加视图时调整其父视图的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1750256/
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
Making view resize to its parent when added with addSubview
提问by Luca
I'm having a problem when using addSubview.
我在使用 addSubview 时遇到问题。
Example code:
示例代码:
ParentView *myParentView = [[ParentView alloc] initWithNibName:@"ParentView " bundle:nil];
ChildeView *myChildeView = [[ChildeView alloc] initWithNibName:@"ChildeView" bundle:nil];
//... parent frame resized with setFrame lets say to x:0, y:0, W:320, H:411
[[myParentView view] addSubview: [myChildeView view]];
My child when added is bigger then the parent, and does not resize its frame to parent bounds. I can't use "clip subviews" on the parent, and "Autoresize Subviews" seems not to work if the parent frame is not resized again. Is there a property that makes a subview resize automatically to its parent's bounds without using setFrame on every child?
添加时我的孩子比父级大,并且不会将其框架调整为父级边界。我不能在父框架上使用“剪辑子视图”,如果父框架没有再次调整大小,“自动调整子视图”似乎不起作用。是否有一个属性可以使子视图自动调整到其父级的边界,而无需在每个子视图上使用 setFrame?
回答by Jeff Kelley
If you aren't using Auto Layout, have you tried setting the child view's autoresize mask? Try this:
如果您没有使用自动布局,您是否尝试过设置子视图的自动调整大小蒙版?尝试这个:
myChildeView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
Also, you may need to call
此外,您可能需要致电
myParentView.autoresizesSubviews = YES;
to get the parent view to resize its subviews automatically when its frame changes.
让父视图在其框架更改时自动调整其子视图的大小。
If you're still seeing the child view drawing outside of the parent view's frame, there's a good chance that the parent view is not clipping its contents. To fix that, call
如果您仍然看到子视图在父视图的框架之外绘制,则父视图很可能没有剪裁其内容。要解决这个问题,请致电
myParentView.clipsToBounds = YES;
回答by cat
Just copy the parent view's frame to the child-view then add it. After that autoresizing will work. Actually you should only copy the size CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)
只需将父视图的框架复制到子视图然后添加它。之后自动调整大小将起作用。其实你应该只复制大小 CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)
childView.frame = CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height);
[parentView addSubview:childView];
回答by Zeesha
Tested in Xcode 9.4, Swift 4Another way to solve this issue is , You can add
在Xcode 9.4, Swift 4 中测试 解决此问题的另一种方法是,您可以添加
override func layoutSubviews() {
self.frame = (self.superview?.bounds)!
}
in subview class.
在子视图类中。
回答by carmen_munich
that's all you need
这就是你所需要的
childView.frame = parentView.bounds
回答by agirault
Swift 4 extension using explicit constraints:
使用显式约束的 Swift 4 扩展:
import UIKit.UIView
extension UIView {
public func addSubview(_ subview: UIView, stretchToFit: Bool = false) {
addSubview(subview)
if stretchToFit {
subview.translatesAutoresizingMaskIntoConstraints = false
leftAnchor.constraint(equalTo: subview.leftAnchor).isActive = true
rightAnchor.constraint(equalTo: subview.rightAnchor).isActive = true
topAnchor.constraint(equalTo: subview.topAnchor).isActive = true
bottomAnchor.constraint(equalTo: subview.bottomAnchor).isActive = true
}
}
}
Usage:
用法:
parentView.addSubview(childView) // won't resize (default behavior unchanged)
parentView.addSubview(childView, stretchToFit: false) // won't resize
parentView.addSubview(childView, stretchToFit: true) // will resize
回答by Sean
You can always do it in your UIViews - (void)didMoveToSuperviewmethod. It will get called when added or removed from your parent (nil when removed). At that point in time just set your size to that of your parent. From that point on the autoresize mask should work properly.
你总是可以在你的 UIViews- (void)didMoveToSuperview方法中做到这一点。从父级添加或删除时会调用它(删除时为零)。那时只需将您的尺寸设置为您父母的尺寸。从那时起,自动调整大小掩码应该可以正常工作。

