ios 改变 UISwitch 的宽度和高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25104605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing UISwitch width and height
提问by Bharat Raichur
I am trying to change the default height and width of a UISwitch element in iOS, but unsuccessfully.
我正在尝试更改 iOS 中 UISwitch 元素的默认高度和宽度,但未成功。
Can you change the default height and width of a UISwitch element?
Should the element be created programmatically?
你能改变 UISwitch 元素的默认高度和宽度吗?
应该以编程方式创建元素吗?
回答by William George
I tested the theory and it appears that you can use a scale transform
to increase the size of the UISwitch
我测试了这个理论,看来您可以使用 ascale transform
来增加UISwitch
UISwitch *aSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(120, 120, 51, 31)];
aSwitch.transform = CGAffineTransformMakeScale(2.0, 2.0);
[self.view addSubview:aSwitch];
回答by Barath
Swift 4
斯威夫特 4
@IBOutlet weak var switchDemo: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
switchDemo.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
}
回答by Warren Burton
Not possible. A UISwitch
has a locked intrinsic height of 51 x 31
.
不可能。AUISwitch
的锁定固有高度为51 x 31
。
You can force constraints on the switch at design time in the xib...
您可以在设计时在 xib...
but come runtime it will snap back to its intrinsic size.
但是到了运行时它会恢复到它的内在大小。
You can supply another image via the .onImage
/ .offImage
properties but again from the docs.
您可以通过.onImage
/.offImage
属性提供另一个图像,但再次来自文档。
The size of this image must be less than or equal to 77 points wide and 27 points tall. If you specify larger images, the edges may be clipped.
此图像的大小必须小于或等于 77 磅宽和 27 磅高。如果指定较大的图像,则边缘可能会被剪裁。
You are going to have to bake your own custom one if you want another size.
如果你想要另一种尺寸,你将不得不烘烤自己的定制尺寸。
回答by Benny Davidovitz
here is a nice UISwitch subclass that i wrote for this purpose, its also IBDesignable so you can control it from your Storyboard / xib
这是我为此目的编写的一个不错的 UISwitch 子类,它也是 IBDesignable,因此您可以从 Storyboard / xib 控制它
@IBDesignable class BigSwitch: UISwitch {
@IBInspectable var scale : CGFloat = 1{
didSet{
setup()
}
}
//from storyboard
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
//from code
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
private func setup(){
self.transform = CGAffineTransform(scaleX: scale, y: scale)
}
override func prepareForInterfaceBuilder() {
setup()
super.prepareForInterfaceBuilder()
}
}
回答by Ivan
Swift 5:
斯威夫特 5:
import UIKit
extension UISwitch {
func set(width: CGFloat, height: CGFloat) {
let standardHeight: CGFloat = 31
let standardWidth: CGFloat = 51
let heightRatio = height / standardHeight
let widthRatio = width / standardWidth
transform = CGAffineTransform(scaleX: widthRatio, y: heightRatio)
}
}