【问题标题】:Having trouble getting UIView sizeToFit to do anything meaningful无法让 UIView sizeToFit 做任何有意义的事情
【发布时间】:2011-04-26 04:33:19
【问题描述】:

当我将子视图添加到 UIView 时,或者当我调整现有子视图的大小时,我希望 [view sizeToFit][view sizeThatFits] 能够反映这种变化。但是,我的经验是sizeToFit 什么都不做,sizeThatFits 在更改前后返回相同的值。

我的测试项目有一个包含单个按钮的视图。单击该按钮会向视图添加另一个按钮,然后在包含视图上调用sizeToFit。视图的边界在添加子视图之前和之后被转储到控制台。

- (void) logSizes {
 NSLog(@"theView.bounds: %@", NSStringFromCGRect(theView.bounds));
 NSLog(@"theView.sizeThatFits: %@", NSStringFromCGSize([theView sizeThatFits:CGSizeZero])); 
}

- (void) buttonTouched { 
 [self logSizes];
 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 btn.frame = CGRectMake(10.0f, 100.0f, 400.0f, 600.0f);
 [theView addSubview:btn];
 [theView sizeToFit];
 [self performSelector:@selector(logSizes) withObject:nil afterDelay:1.0];
}

输出是:

2010-10-15 15:40:42.359 SizeToFit[14953:207] theView.bounds: {{0, 0}, {322, 240}}
2010-10-15 15:40:42.387 SizeToFit[14953:207] theView.sizeThatFits: {322, 240}
2010-10-15 15:40:43.389 SizeToFit[14953:207] theView.bounds: {{0, 0}, {322, 240}}
2010-10-15 15:40:43.391 SizeToFit[14953:207] theView.sizeThatFits: {322, 240}

我一定是错过了什么。

谢谢。

【问题讨论】:

    标签: iphone ios uiview resize uibutton


    【解决方案1】:

    文档对此非常清楚。 -sizeToFit 几乎调用了-sizeThatFits:(可能以视图的当前大小作为参数),而-sizeThatFits: 的默认实现几乎什么都不做(它只是返回它的参数)。

    一些 UIView 子类会覆盖 -sizeThatFits: 来做一些更有用的事情(例如 UILabel)。如果您想要任何其他功能(例如调整视图大小以适应其子视图),您应该继承 UIView 并覆盖 -sizeThatFits:

    【讨论】:

    • 谢谢奥莱。应该更仔细地阅读文档,而不是查看依赖于 UIView 特定子类中被覆盖行为的代码 sn-ps。
    • 我很惊讶这里没有答案可以分享他们对这种覆盖的实现。
    【解决方案2】:

    如果你不覆盖 UIView,你可以使用扩展。

    斯威夫特:

    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
        }
    
    }
    

    相同的代码,但速度快了 3 倍:

    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)
        }
    }
    

    【讨论】:

      【解决方案3】:

      你可以单独使用 IB (xcode 4.5) 做一些类似的事情:

      1. 点击UIView
      2. 在大小检查器中将 content hugging 拖到 1(水平和垂直)
      3. compression resistance 拖动到 1000(两者都适用)
      4. 在 UIView 的 constraints 下点击 Width 并将 priority 更改为 250
      5. 对身高做同样的事情
      6. 您可以使用UIViewinset 来控制左/右/上/下的填充

      【讨论】:

        【解决方案4】:
                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;
        

        这对我有用。

        【讨论】:

          猜你喜欢
          • 2012-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-10
          • 2012-09-07
          • 2016-10-02
          • 2013-09-28
          • 1970-01-01
          相关资源
          最近更新 更多