xcode 如何调整 UISwitch 按钮的大小?

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

How to resize UISwitch button?

iphoneiosxcodeuitableviewuiswitch

提问by josh

I want to resize my UISwitchbutton which is attach on UITableView. I found some help on google and did this successfully using CGAffineTransformMakeScalebut with this I am getting an Issue when I change position this switch button it goes its own original size may be because its on table view but I am resizing this in ViewDidLoaddelegate. Here is what I am doing.

我想调整UISwitch附加在UITableView. 我在谷歌上找到了一些帮助并成功使用CGAffineTransformMakeScale了这个,但是当我改变这个开关按钮的位置时,我遇到了一个问题,它变成了它自己的原始大小,可能是因为它在表格视图上,但我正在ViewDidLoad委托中调整它的大小。这就是我正在做的事情。

- (void)viewDidLoad{
 switchFB = [[UISwitch alloc] initWithFrame:CGRectMake(227, 8, 79, 27)];
switchFB.transform= CGAffineTransformMakeScale(0.7, 0.7);}

and in Cell for row at index path

并在单元格中索引路径处的行

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{`static NSString *CellIdentifier = @"SettingsCell";`

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }

Kindly check this where I am doing wrong and if my procedure is not right way so can you please suggest me some better way to do this. This will be great for me. Thanks in advance.

请检查这个我做错的地方,如果我的程序不正确,那么你能建议我一些更好的方法来做到这一点。这对我来说会很棒。提前致谢。

回答by Minakshi

Try this

尝试这个

 UISwitch *mySwitch = [UISwitch new];
mySwitch.transform = CGAffineTransformMakeScale(0.75, 0.75);

回答by henryaz

In iOS 8, I have successfully resized UISwitches using a custom container view. Code looks something like this:

在 iOS 8 中,我使用自定义容器视图成功调整了 UISwitches 的大小。代码如下所示:

@interface MyContainerView : UIView 

@end

@implementation MyContainerView

- (void)layoutSubviews
{
    [super layoutSubviews];

    // Center my subviews so the transform views properly
    CGPoint c = CGPointCenterOfRect(self.bounds);
    for (UIView * v in self.subviews)
    {
        v.center = c;
    }

}
@end


UISwitch  * switchFB = [[UISwitch alloc] initWithFrame:CGRectZero];
switchFB.transform= CGAffineTransformMakeScale(0.7, 0.7);

CGSize s = switchFB.intrinsicContentSize;
CGRect r = CGRectMake(0,0,s.width, s.height);
MyContainerView * v = [[MyContainerView alloc] initWithFrame:r];

[v addSubview:switchFB];

The purpose of the container view is two fold: - You have a handle on something that can auto layout correctly - You can subclass the layoutSubviews and recenter your transform'd control automatically after the builtin auto layout has attempted its thing.

容器视图的目的有两个: - 您可以处理可以正确自动布局的东西 - 您可以将 layoutSubviews 子类化,并在内置自动布局尝试其操作后自动重新调整您的转换控件。

Note that UISwitch does adjust its intrinsic content size when it is transformed, and i use this to set the frame of the container view.

请注意, UISwitch 在转换时会调整其内在内容大小,我使用它来设置容器视图的框架。

回答by Sethmr

I would recommend trying out this library that I wrote. I have a readme for it to make it easy to figure out how to use. SwiftySwitchIt allows you to make the switch any size you want. Even if you don't want to use the library, you could see how I made a Custom Switch.

我建议尝试一下我写的这个库。我有一个自述文件,可以很容易地弄清楚如何使用。SwiftySwitch它允许您将开关设置为任何您想要的大小。即使你不想使用这个库,你也可以看到我是如何制作自定义开关的。