繁体   English   中英

iPhone UIView - 调整框架以适应子视图

[英]iPhone UIView - Resize Frame to Fit Subviews

添加子视图后,是否应该有一种方法来调整 UIView 的框架大小,以便框架是包含所有子视图所需的大小? 如果您的子视图是动态添加的,您如何确定容器视图的框架需要多大? 这不起作用:

[myView sizeToFit];

您还可以添加以下代码来计算子视图位置。

[myView resizeToFitSubviews]

UIViewUtils.h

#import <UIKit/UIKit.h>

@interface UIView (UIView_Expanded)

-(void)resizeToFitSubviews;

@end

UIViewUtils.m

#import "UIViewUtils.h"

@implementation UIView (UIView_Expanded)

-(void)resizeToFitSubviews
{
    float w = 0;
    float h = 0;

    for (UIView *v in [self subviews]) {
        float fw = v.frame.origin.x + v.frame.size.width;
        float fh = v.frame.origin.y + v.frame.size.height;
        w = MAX(fw, w);
        h = MAX(fh, h);
    }
    [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, w, h)];
}

@end

我需要让子视图有一个负的原点,而CGRectUnion是 ObjC 的方法,老实说,正如评论中提到的那样。 首先,让我们看看它是如何工作的:

正如你在下面看到的,我们假设一些子视图在外面,所以我们需要做两件事来让它看起来很好,而不影响子视图的定位:

  1. 将视图的框架移动到最左上角的位置
  2. 将子视图向相反方向移动以消除效果。

一张图片胜过千言万语。

示范

代码价值十亿字。 这是解决方案:

@interface UIView (UIView_Expanded)

- (void)resizeToFitSubviews;

@end

@implementation UIView (UIView_Expanded)

- (void)resizeToFitSubviews
{
    // 1 - calculate size
    CGRect r = CGRectZero;
    for (UIView *v in [self subviews])
    {
        r = CGRectUnion(r, v.frame);
    }

    // 2 - move all subviews inside
    CGPoint fix = r.origin;
    for (UIView *v in [self subviews])
    {
        v.frame = CGRectOffset(v.frame, -fix.x, -fix.y);
    }

    // 3 - move frame to negate the previous movement
    CGRect newFrame = CGRectOffset(self.frame, fix.x, fix.y);
    newFrame.size = r.size;

    [self setFrame:newFrame];
}

@end

我认为用 Swift 2.0 编写会很有趣..我是对的!

extension UIView {

    func resizeToFitSubviews() {

        let subviewsRect = subviews.reduce(CGRect.zero) {
            $0.union($1.frame)
        }

        let fix = subviewsRect.origin
        subviews.forEach {
            $0.frame.offsetInPlace(dx: -fix.x, dy: -fix.y)
        }

        frame.offsetInPlace(dx: fix.x, dy: fix.y)
        frame.size = subviewsRect.size
    }
}

和操场证明:

请注意, visualAidView不会移动,它可以帮助您查看superview如何在保持子视图位置superview调整大小。

let canvas = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
canvas.backgroundColor = UIColor.whiteColor()

let visualAidView = UIView(frame: CGRect(x: 5, y: 5, width: 70, height: 70))
visualAidView.backgroundColor = UIColor(white: 0.8, alpha: 1)
canvas.addSubview(visualAidView)

let superview = UIView(frame: CGRect(x: 15, y: 5, width: 50, height: 50))
superview.backgroundColor = UIColor.purpleColor()
superview.clipsToBounds = false
canvas.addSubview(superview)

[
    {
        let view = UIView(frame: CGRect(x: -10, y: 0, width: 15, height: 15))
        view.backgroundColor = UIColor.greenColor()
        return view
    }(),
    {
        let view = UIView(frame: CGRect(x: -10, y: 40, width: 35, height: 15))
        view.backgroundColor = UIColor.cyanColor()
        return view
    }(),
    {
        let view = UIView(frame: CGRect(x: 45, y: 40, width: 15, height: 30))
        view.backgroundColor = UIColor.redColor()
        return view
    }(),

].forEach { superview.addSubview($0) }

游乐场图片

看起来上面肯尼的回答指向了引用问题中的正确解决方案,但可能带走了错误的概念。 UIView类参考明确提出了一种系统,可以使sizeToFit与您的自定义视图相关。

覆盖sizeThatFits ,而不是sizeToFit

您的自定义UIView需要覆盖sizeThatFits以返回“适合接收者子视图的新大小”,但是您希望计算它。 您甚至可以使用另一个答案中的数学来确定您的新尺寸(但无需重新创建内置sizeToFit系统)。

sizeThatFits返回与其状态相关的数字后,在自定义视图上调用sizeToFit将开始导致预期的调整大小。

sizeThatFits工作原理

如果没有覆盖, sizeThatFits只返回传入的大小参数(默认为self.bounds.size来自sizeToFit调用。虽然我只有几个关于这个问题的来源,但似乎看不到传入的大小作为严格的要求。

[ sizeThatFits ] 返回适合传递给它的约束的控件的“最合适”大小。 如果无法满足约束,该方法可以(强调他们的)决定忽略这些约束。

查看无法让 UIView sizeToFit 做任何有意义的事情

要点是sizeToFit是供子类覆盖的,并且在 UIView 中不做任何事情。 它为UILabel做一些事情,因为它覆盖了sizeThatFits:sizeToFit

更新了@Mazyod 对 Swift 3.0 的回答,效果很好!

extension UIView {

func resizeToFitSubviews() {

    let subviewsRect = subviews.reduce(CGRect.zero) {
        $0.union($1.frame)
    }

    let fix = subviewsRect.origin
    subviews.forEach {
        $0.frame.offsetBy(dx: -fix.x, dy: -fix.y)
        }

        frame.offsetBy(dx: fix.x, dy: fix.y)
        frame.size = subviewsRect.size
    }
}

老问题,但你也可以用递归函数来做到这一点。

您可能想要一个无论有多少子视图和子子视图都始终有效的解决方案,...

更新:前一段代码只有一个 getter 函数,现在也是一个 setter。

extension UIView {

    func setCGRectUnionWithSubviews() {
        frame = getCGRectUnionWithNestedSubviews(subviews: subviews, frame: frame)
        fixPositionOfSubviews(subviews, frame: frame)
    }

    func getCGRectUnionWithSubviews() -> CGRect {
        return getCGRectUnionWithNestedSubviews(subviews: subviews, frame: frame)
    }

    private func getCGRectUnionWithNestedSubviews(subviews subviews_I: [UIView], frame frame_I: CGRect) -> CGRect {

        var rectUnion : CGRect = frame_I
        for subview in subviews_I {
            rectUnion = CGRectUnion(rectUnion, getCGRectUnionWithNestedSubviews(subviews: subview.subviews, frame: subview.frame))
        }
        return rectUnion
    }

    private func fixPositionOfSubviews(subviews: [UIView], frame frame_I: CGRect) {

        let frameFix : CGPoint = frame_I.origin
        for subview in subviews {
            subview.frame = CGRectOffset(subview.frame, -frameFix.x, -frameFix.y)
        }
    }
}

这是已接受答案的快速版本,也是很小的变化,而不是扩展它,此方法将视图作为变量获取并返回。

func resizeToFitSubviews(#view: UIView) -> UIView {
    var width: CGFloat = 0
    var height: CGFloat = 0

    for someView in view.subviews {
        var aView = someView as UIView
        var newWidth = aView.frame.origin.x + aView.frame.width
        var newHeight = aView.frame.origin.y + aView.frame.height
        width = max(width, newWidth)
        height = max(height, newHeight)
    }

    view.frame = CGRect(x: view.frame.origin.x, y: view.frame.origin.y, width: width, height: height)
    return view
}

这是扩展版本:

/// Extension, resizes this view so it fits the largest subview
func resizeToFitSubviews() {
    var width: CGFloat = 0
    var height: CGFloat = 0
    for someView in self.subviews {
        var aView = someView as! UIView
        var newWidth = aView.frame.origin.x + aView.frame.width
        var newHeight = aView.frame.origin.y + aView.frame.height
        width = max(width, newWidth)
        height = max(height, newHeight)
    }

    frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: width, height: height)
}

任何动态添加所有这些子视图的模块都必须知道将它们放在哪里(以便它们正确关联,或者它们不会重叠等)一旦你知道了,加上当前视图的大小,加上当前视图的大小子视图,您拥有了确定是否需要修改封闭视图所需的一切。

当您创建自己的 UIView 类时,请考虑覆盖子类中的 IntrinsicContentSize。 操作系统调用此属性以了解建议的视图大小。 我将把 C# (Xamarin) 的代码片段,但想法是一样的:

[Register("MyView")]
public class MyView : UIView
{
    public MyView()
    {
        Initialize();
    }

    public MyView(RectangleF bounds) : base(bounds)
    {
        Initialize();
    }

    Initialize()
    {
        // add your subviews here.
    }

    public override CGSize IntrinsicContentSize
    {
        get
        {
            return new CGSize(/*The width as per your design*/,/*The height as per your design*/);
        }
    }
}

返回的大小完全取决于您的设计。 例如,如果您刚刚添加了一个标签,那么宽度和高度就是该标签的宽度和高度。 如果您有更复杂的视图,则需要返回适合所有子视图的大小。

尽管已经有很多答案,但没有一个适用于我的用例。 如果您使用 autoresizingMasks 并想要调整视图大小并移动子视图,以便矩形的大小与子视图匹配。

public extension UIView {

    func resizeToFitSubviews() {
        var x = width
        var y = height
        var rect = CGRect.zero
        subviews.forEach { subview in
            rect = rect.union(subview.frame)
            x = subview.frame.x < x ? subview.frame.x : x
            y = subview.frame.y < y ? subview.frame.y : y
        }
        var masks = [UIView.AutoresizingMask]()
        subviews.forEach { (subview: UIView) in
            masks.add(subview.autoresizingMask)
            subview.autoresizingMask = []
            subview.frame = subview.frame.offsetBy(dx: -x, dy: -y)
        }
        rect.size.width -= x
        rect.size.height -= y
        frame.size = rect.size
        subviews.enumerated().forEach { index, subview in
            subview.autoresizingMask = masks[index]
        }
    }
}
[myView sizeToFit];

应该工作,你为什么不检查 CGRect 前后?

暂无
暂无

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

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