Ios 为什么。中心对我来说只在y轴上工作?

Ios 为什么。中心对我来说只在y轴上工作?,ios,swift,Ios,Swift,运行此命令后,我的whiteBoxSelector将移动到friends按钮,但仅在y轴上,x位置似乎为0。我没有为白框设置任何约束,我的按钮位于堆栈视图中。视图的中心属性指定: 框架的中心。在坐标中指定中心 它的superview系统,以点为单位测量。设置此 属性相应地更改框架属性的值 您的问题是,whiteBoxSelector和friendsButton不共享相同的superview,因此坐标系不相同。您需要将friendsButton的中心从friendsButton(其超级视图)的坐标

运行此命令后,我的
whiteBoxSelector
将移动到friends按钮,但仅在y轴上,x位置似乎为0。我没有为白框设置任何约束,我的按钮位于堆栈视图中。

视图的中心属性指定:

框架的中心。

在坐标中指定中心 它的superview系统
,以点为单位测量。设置此 属性相应地更改框架属性的值

您的问题是,
whiteBoxSelector
friendsButton
不共享相同的
superview
,因此坐标系不相同。您需要将
friendsButton
中心
friendsButton
(其超级视图)的坐标系转换为
whiteBoxSelector
(其超级视图)的坐标系

class ViewController: UIViewController {
    @IBOutlet var friendsButton: UIImageView! //Friends Button
    @IBOutlet var circleButton: UIImageView! //Circle Button
    @IBOutlet var profileButton: UIImageView! //Profile Button
    @IBOutlet var whiteBoxSelector: UIImageView! //White Box Selector

    override func viewDidLayoutSubviews() {
        whiteBoxSelector.center = self.friendsButton.center //line that doesn't seem to run properly
    }
}
override func viewDidLayoutSubviews() {
    whiteBoxSelector.center = whiteBoxSelector.superview.convert(friendsButton.center, from: friendsButton.superview)
}