简体   繁体   English

centerXAnchor和center.x之间的区别

[英]Difference between centerXAnchor and center.x

As far as I know the two main ways to center an element horizontally is to use the following techniques: 据我所知,水平居中元素的两种主要方法是使用以下技术:

1. 1。

label.center.x = imageView.center.x

2. 2。

label.centerXAnchor.constraint(equalTo: imageView.centerXAnchor)

What is the difference, if any, between these two methods? 这两种方法之间有什么区别?

1.Following sets up a label's frame 1.以下设置标签的框架

label.center.x = imageView.center.x

2.Following sets up an auto layout constraint for a label 2.Folllowing为标签设置自动布局约束

label.centerXAnchor.constraint(equalTo: imageView.centerXAnchor)

If you are not familiar with an autolayout check an Apple's Auto Layout Guide 如果您不熟悉自动布局,请查看Apple的“ 自动布局指南”

When you are referring to center property, you are getting the CGPoint reference of this, it is a low-level way of positioning things in your application. 当您引用中心属性时,您将获得CGPoint参考,这是一种在应用程序中定位内容的低级方法。 When you are working with centerXAnchor, you are using the Autolayout method to positioning things, as you know Auto Layout dynamically calculates the size and position of all the views in your view hierarchy, based on constraints placed on those views. 当您使用centerXAnchor时,您正在使用Autolayout方法来定位事物,因为您知道Auto Layout会根据对这些视图的约束动态计算视图层次结构中所有视图的大小和位置。

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

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