Having trouble getting UIView sizeToFit to do anything meaningful

后端 未结 4 632
陌清茗
陌清茗 2020-12-09 02:07

When I add a subview to a UIView, or when I resize an existing subview, I would expect [view sizeToFit] and [view sizeThatFits] to ref

相关标签:
4条回答
  • 2020-12-09 02:29

    If you won't override UIView, u can just use extension.

    Swift:

    extension UIView {
    
        func sizeToFitCustom () {
            var size = CGSize(width: 0, height: 0)
            for view in self.subviews {
                let frame = view.frame
                let newW = frame.origin.x + frame.width
                let newH = frame.origin.y + frame.height
                if newW > size.width {
                    size.width = newW
                }
                if newH > size.height {
                    size.height = newH
                }
            }
            self.frame.size = size
        }
    
    }
    

    The same code but 3 times faster:

    extension UIView {
        final func sizeToFitCustom() {
            var w: CGFloat = 0,
                h: CGFloat = 0
            for view in subviews {
                if view.frame.origin.x + view.frame.width > w { w = view.frame.origin.x + view.frame.width }
                if view.frame.origin.y + view.frame.height > h { h = view.frame.origin.y + view.frame.height }
            }
            frame.size = CGSize(width: w, height: h)
        }
    }
    
    0 讨论(0)
  • 2020-12-09 02:34
            self.errorMessageLabel.text = someNewMessage;
    
        // We don't know how long the given error message might be, so let's resize the label + containing view accordingly
        CGFloat heightBeforeResize = self.errorMessageLabel.frame.size.height;
    
        [self.errorMessageLabel sizeToFit];
    
        CGFloat differenceInHeightAfterResize = self.errorMessageLabel.frame.size.height - heightBeforeResize;
    
        self.errorViewHeightContstraint.constant = kErrorViewHeightConstraintConstant + differenceInHeightAfterResize;
    

    This worked for me.

    0 讨论(0)
  • 2020-12-09 02:36

    You can do some like that using IB alone (xcode 4.5):

    1. Click on the UIView
    2. in the Size inspector drag content hugging to 1 (both horizontal and vertical)
    3. drag compression resistance to 1000 (for both)
    4. under the UIView's constraints click on Width and change priority to 250
    5. Do the same for Height
    6. You can use the UIView's inset to control padding for left/right/top/bottom
    0 讨论(0)
  • 2020-12-09 02:48

    The documentation is pretty clear on this. -sizeToFit pretty much calls -sizeThatFits: (probably with the view's current size as the argument), and the default implementation of -sizeThatFits: does almost nothing (it just returns its argument).

    Some UIView subclasses override -sizeThatFits: to do something more useful (e.g. UILabel). If you want any other functionality (such as resizing a view to fit its subviews), you should subclass UIView and override -sizeThatFits:.

    0 讨论(0)
提交回复
热议问题