对齐 UISwitch 以居中对齐
Align UISwitch to align to center
我下面的 swift 代码试图让 uisiwtch 出现在屏幕中央。正如你在我下面的照片中看到的那样。如下图所示。开关只占据了屏幕左上角的一小部分。它看起来不太好。
pic
var soundSwitch = UISwitch()
NSLayoutConstraint.Activate[(
soundSwitch.leadingAnchor.constraint(equalTo: map.trailingAnchor, constant: 0),
soundSwitch.topAnchor.constraint(equalTo: text.bottomAnchor, constant : 0),
soundSwitch.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1/3, constant: 0),
soundSwitch.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.07, constant: 0),
)]
替换
soundSwitch.leadingAnchor.constraint(equalTo: map.trailingAnchor, constant: 0),
与
soundSwitch.centerXAnchor.constraint(equalTo: map.centerXAnchor, constant: 0),
顺便说一句,别忘了
soundSwitch.translatesAutoresizingMaskIntoConstraints = false
此外,最好同时删除两者
soundSwitch.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1/3, constant: 0),
soundSwitch.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.07, constant: 0),
因为开关不会缩放
我下面的 swift 代码试图让 uisiwtch 出现在屏幕中央。正如你在我下面的照片中看到的那样。如下图所示。开关只占据了屏幕左上角的一小部分。它看起来不太好。
pic
var soundSwitch = UISwitch()
NSLayoutConstraint.Activate[(
soundSwitch.leadingAnchor.constraint(equalTo: map.trailingAnchor, constant: 0),
soundSwitch.topAnchor.constraint(equalTo: text.bottomAnchor, constant : 0),
soundSwitch.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1/3, constant: 0),
soundSwitch.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.07, constant: 0),
)]
替换
soundSwitch.leadingAnchor.constraint(equalTo: map.trailingAnchor, constant: 0),
与
soundSwitch.centerXAnchor.constraint(equalTo: map.centerXAnchor, constant: 0),
顺便说一句,别忘了
soundSwitch.translatesAutoresizingMaskIntoConstraints = false
此外,最好同时删除两者
soundSwitch.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1/3, constant: 0),
soundSwitch.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.07, constant: 0),
因为开关不会缩放