如何调整UISwitch按钮的大小?
问题描述:
我想调整我的UISwitch
按钮的大小,该按钮附在UITableView
上。我在谷歌上找到了一些帮助,并成功地使用了CGAffineTransformMakeScale
,但使用这个我得到一个问题,当我改变位置这个开关按钮时,它变成了它自己的原始尺寸,可能是因为它在桌面视图上,但我在ViewDidLoad
代表中调整了这个尺寸。这是我正在做的。如何调整UISwitch按钮的大小?
- (void)viewDidLoad{
switchFB = [[UISwitch alloc] initWithFrame:CGRectMake(227, 8, 79, 27)];
switchFB.transform= CGAffineTransformMakeScale(0.7, 0.7);}
,并在索引路径细胞为行
- (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;
}
请检查这个地方我做错了,如果我的方法是不正确的方式,以便可以请你建议我一些更好的方式来做到这一点。这对我来说很好。提前致谢。
答
尝试使用自定义容器针对这一
UISwitch *mySwitch = [UISwitch new];
mySwitch.transform = CGAffineTransformMakeScale(0.75, 0.75);
答
在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];
容器视图的目的是双重的: - 你有一个把手上的东西,可以自动排列正确 - 你也可以继承的layoutSubviews和recenter您transform'd控制在内置自动布局尝试完成之后自动进行。
请注意,UISwitch在进行转换时会调整内在内容的大小,并使用它来设置容器视图的框架。
答
我会推荐尝试一下我写的这个库。我有一个自述文件,可以很容易地找出如何使用。它可以让你做任何你想要的大小的开关。即使你不想使用这个库,你也可以看到我是如何制作一个自定义开关的。
你有没有仔细阅读我的问题,巴迪我正在做同样的事情你在暗示。 – josh 2013-03-04 07:35:40
是的,您需要根据您的需要更改0.75。这是什么为我工作mySwitch.transform = CGAffineTransformMakeScale(1.25,1.1);我在解决上述问题时遇到了同样的问题。 – Minakshi 2013-03-04 08:04:21
我也在做同样的事情在视图加载方法,因为我提到过。 switchFB.transform = CGAffineTransformMakeScale(0.7,0.7);}它也调整开关按钮大小。 – josh 2013-03-04 08:19:03