简体   繁体   English

UIView的frame.height和frame.size.height有什么区别?

[英]What's the difference between UIView's frame.height and frame.size.height?

I don't understand what the difference is in a UIView between frame.height and frame.size.height . 我不明白frame.heightframe.size.height之间的UIView什么区别。 Why add size ? 为什么要加大size For example: 例如:

override func viewDidLoad() {
    super.viewDidLoad()

    scrollView.contentSize.height = self.view.frame.height
    scrollViewHeight = scrollView.frame.size.height
}

It all started with Objective-C and CGRect . 这一切都始于Objective-C和CGRect It only has two properties - origin and size . 它只有两个属性 - originsize size is a CGSize which in turn has a height property. size是一个CGSize ,后者又有一个height属性。 So it is common in Objective-C to get the height of a frame using: 所以在Objective-C中常见的是使用以下方法获取帧的高度:

CGFloat height = frame.size.height;

Technically, the correct solution (to get a proper, normalized height) is to use: 从技术上讲,正确的解决方案(获得正确的标准化高度)是使用:

CGFloat height = CGRectGetHeight(frame);

Then Swift came along and CGRect became a Swift struct . 然后Swift出现了, CGRect成了一个Swift struct While it also has a size property of type CGSize which in turn has a height property, the Swift CGRect has a height property. 虽然它还具有CGSize类型的size属性,而该属性又具有height属性,但Swift CGRect具有height属性。 In Swift, the two lines above become: 在Swift中,上面的两行成为:

let height = frame.size.height;

and

let height = frame.height;

In most cases, there is no difference between the two ways to get the height of a frame. 在大多数情况下,获得帧高度的两种方法之间没有区别。 But there is a difference if the frame has a negative height. 但是如果框架具有负高度则存在差异。

Example: 例:

let rect = CGRect(x: 0, y: 0, width: 320, height: -40)
print(rect.height) // 40
print(rect.size.height) // -40

Accessing size.height gives the raw value used when the CGRect was created. 访问size.height会给出创建CGRect时使用的原始值。 Access height gives a normalized value of the height. 访问height给出了标准化的高度值。

In most cases you should use the height property and not size.height . 在大多数情况下,您应该使用height属性而不是size.height But more importantly, be consistent. 但更重要的是,要保持一致。 The code in your question is inconsistent. 您问题中的代码不一致。

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

相关问题 不论视图高度如何,frame.size.height的值始终相同 - Value for frame.size.height is always the same regardless of height of view iOS-自定义视图frame.size.height给出0值 - iOS - Custom view frame.size.height gives a 0 value Swift 4-使用负数编辑标签frame.size.height - Swift 4 - Editing label frame.size.height with negative number 来自 frame.height 的 CGFloat 值 - CGFloat value from frame.height 将视图控制器frame.height和frame.width交换为iOS 8 - View controller frame.height and frame.width are swapped for iOS 8 当UITableViewController frame.height很小时,UITableViewController refreshControl很麻烦 - UITableViewController refreshControl is glitchy when UITableViewController frame.height is small 当我改变UIVIew的bounds.size.height时,frame.origin.y会改变 - the frame.origin.y change on when I change UIVIew's bounds.size.height 尺寸类中紧凑宽度的“常规高度”和“任何高度”之间有什么区别? - What's the difference between “Regular Height” and “Any Height” in Compact Width in Size Classes? 调整与UILabel的框架高度成比例的NSMutableAttributedString字体大小 - Adjust font size of NSMutableAttributedString proportional to UILabel's frame height 如何使UICollectionViewCell的高度填充框架的所有大小? - How to make a UICollectionViewCell's height to fill all the size of the frame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM