【发布时间】:2011-02-26 07:37:18
【问题描述】:
我做了一个custom UISwitch (from this post)。但问题是,我的自定义文本有点长。有没有办法调整开关的大小? [我试过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];
//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"];
【问题讨论】:
-
您使用的是 UISwitch(来自您的主题)还是 UICustomSwitch(来自您的链接)? UICustomSwitch class 实际上是一个 UISlider。
-
但是继承了UISwitch,怎么可能是滑块?
-
UICustomSwitch 继承自 UISlider。你用的是哪一个?您的代码说 CustomUISwitch,您的主题说 UISwitch,但是您给出的链接指向 UICustomSwitch 实现?我们无法很好地回答您的问题,除非您说出您的真正意思是哪三个!
-
对不起,我已经添加了代码。
标签: iphone cocoa cocoa-touch