xcode UISwitch 自定义大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29606221/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:58:16  来源:igfitidea点击:

UISwitch Custom Size

iosxcodeswiftuiswitch

提问by Kashif

I have created a UISwitchin the Interface Builder. Its default size is 51x31

UISwitch在 Interface Builder 中创建了一个。它的默认大小是 51x31

I have put constraints on it to make it smaller size 33x20

我对其进行了限制,使其尺寸更小 33x20

It shows smaller in the Interface Builder but on runtime it does not obey to the size constraints.

它在 Interface Builder 中显示较小,但在运行时它不遵守大小限制。

Elsewhere is it told that UISwitchis a special control and does not like to obey size constraints. Is it true?

在其他地方被告知这UISwitch是一个特殊的控件并且不喜欢遵守大小限制。这是真的吗?

Are there any workarounds?

有什么解决方法吗?

回答by Bharath Vankireddy

As simple instead of creating a UISwitch, create UIButtonand set background image as you want. Here set 2 background images for UIButtonfor 2 states. Just toggle the UIButtonbetween these 2 states

简单而不是创建一个UISwitch,根据需要创建UIButton和设置背景图像。这里UIButton为 2 个状态设置了 2 个背景图像。只需UIButton在这两种状态之间切换