如何调整UISwitch的大小? [英] How to resize a UISwitch?
本文介绍了如何调整UISwitch的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已自订UISwitch(来自这篇文章)。但问题是,我的自定义文本有点长。有什么办法来调整交换机的大小吗? [我尝试 setBounds
,无效]
编辑
这里是我使用的代码:
@interface CustomUISwitch:UISwitch
- (void)setLeftLabelText:(NSString *)labelText;
- (void)setRightLabelText:(NSString *)labelText;
@end
@implementation CustomUISwitch
- (UIView *)slider
{
return [[self subviews] lastObject];
}
- (UIView *)textHolder
{
return [[[self slider] subviews] objectAtIndex:2];
}
- (UILabel *)leftLabel
{
return [[[self textHolder] subviews] objectAtIndex:0];
}
- (UILabel *)rightLabel
{
return [[[self textHolder] subviews] objectAtIndex:1];
}
- (void)setLeftLabelText:(NSString *)labelText
{
[[self leftLabel] setText:labelText];
}
- (void)setRightLabelText:(NSString *)labelText
{
[[self rightLabel] setText:labelText];
}
@end
mySwitch = [[CustomUISwitch alloc] initWithFrame:CGRectZero];
//尝试这些,但没有工作
// CGRect aFrame = mySwitch.frame;
//aFrame.size.width = 200;
//aFrame.size.height = 100;
//mySwitch.frame = aFrame;
[mySwitch setLeftLabelText:@longValue1];
[mySwitch setRightLabelText:@longValue2];
解决方案
最简单的方法是调整它的大小:
UISwitch * mySwitch = [[UISwitch alloc] init];
mySwitch.transform = CGAffineTransformMakeScale(0.75,0.75);
,你不必关心别的事!
I have made a custom UISwitch (from this post). But the problem is, my custom texts are a bit long. Is there any way to resize the switch? [I tried setBounds
, did not work]
Edit:
Here is the code I used:
@interface CustomUISwitch : UISwitch
- (void) setLeftLabelText: (NSString *) labelText;
- (void) setRightLabelText: (NSString *) labelText;
@end
@implementation CustomUISwitch
- (UIView *) slider
{
return [[self subviews] lastObject];
}
- (UIView *) textHolder
{
return [[[self slider] subviews] objectAtIndex:2];
}
- (UILabel *) leftLabel
{
return [[[self textHolder] subviews] objectAtIndex:0];
}
- (UILabel *) rightLabel
{
return [[[self textHolder] subviews] objectAtIndex:1];
}
- (void) setLeftLabelText: (NSString *) labelText
{
[[self leftLabel] setText:labelText];
}
- (void) setRightLabelText: (NSString *) labelText
{
[[self rightLabel] setText:labelText];
}
@end
mySwitch = [[CustomUISwitch alloc] initWithFrame:CGRectZero];
//Tried these, but did not work
//CGRect aFrame = mySwitch.frame;
//aFrame.size.width = 200;
//aFrame.size.height = 100;
//mySwitch.frame = aFrame;
[mySwitch setLeftLabelText: @"longValue1"];
[mySwitch setRightLabelText: @"longValue2"];
解决方案
The simplest way is to resize it, as a view:
UISwitch *mySwitch = [[UISwitch alloc] init];
mySwitch.transform = CGAffineTransformMakeScale(0.75, 0.75);
and you don't have to care about anything else!
这篇关于如何调整UISwitch的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文