53

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"];
Community
  • 1
  • 1
mshsayem
  • 17,557
  • 11
  • 61
  • 69
  • Are you using UISwitch (from your subject) or UICustomSwitch (from your link)? The [UICustomSwitch class](http://www.catamount.com/blog/?p=1063) is actually a UISlider. – progrmr Jun 01 '10 at 13:15
  • but it inherits UISwitch, how can it be a slider? – mshsayem Jun 01 '10 at 14:06
  • UICustomSwitch inherits from UISlider. Which one are you using? Your code says CustomUISwitch, your subject says UISwitch, but the link you gave points to a UICustomSwitch implementation? We can't answer your question very well unless you say which of the 3 you really meant! – progrmr Jun 01 '10 at 14:20
  • Sorry, Now I have added the code. – mshsayem Jun 01 '10 at 14:23

8 Answers8

189

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!

sunkehappy
  • 8,970
  • 5
  • 44
  • 65
mxg
  • 20,946
  • 12
  • 59
  • 80
28

Here is the swift 3 version of mxg answer:

let mySwitch = UISwitch()
mySwitch.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
Beninho85
  • 3,273
  • 27
  • 22
7

Many controls are meant to be a specific size. If you were implementing this yourself, you would override setFrame:, adjust the frame parameter to match your control's required size, and then pass that to [super setFrame:].

If you subclass a control that has this behavior, there's really no way to override it because your subclass will inherit the superclass's implementation of setFrame:, which modifies your frame rectangle. And there's no way to set the frame of your control without calling [super setFrame:].

You'll most likely have to inherit from UIControl and implement the properties/behaviors you want from UISwitch manually to work around this.

Alex
  • 26,829
  • 3
  • 55
  • 74
  • 1
    Confirmed. Went to UISwitch.h... it has this comment in initWithFrame: "// This class enforces a size appropriate for the control. The frame size is ignored." – mshsayem Jun 01 '10 at 14:32
4

UISwitch is not designed to be customized.

I think the your best solution is to download one of the custom switch implementations mentioned in the other question that you referred to. Either UICustomSwitch or RCSwitch. They both should be able to handle wide labels.

progrmr
  • 75,956
  • 16
  • 112
  • 147
  • I used UICustomSwitch here... but can't resize it... long labels become wrapped.. RCSwitch works.. thanks – mshsayem Jun 01 '10 at 14:44
3

There is no option for resizing uiswitch in xib, so need to create and resize it in controller class,

     UISwitch *onoff = [[UISwitch alloc] initWithFrame: CGRectMake(0, 0, 10, 10)];
     onoff.transform = CGAffineTransformMakeScale(0.50, 0.50);
     [self.view addSubview:onoff];
saba
  • 430
  • 3
  • 14
2

If you want to resize switch put through the Storyboard or nib, You can subclass UISwitch and override awakeFromNib method:

- (void)awakeFromNib {
    self.transform = CGAffineTransformMakeScale(0.75, 0.75);
}

Select the switch control and change it's class to your custom switch class.

enter image description here

Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63
1

Here is a solution in code:

UISwitch *mySwitchNewsletter = [[UISwitch alloc] initWithFrame: CGRectMake(varSettingsSwitchNewsletter_x, 
                                                                           varSettingsSwitchNewsletter_y, 
                                                                           varSettingsSwitchNewsletter_width, 
                                                                           varSettingsSwitchNewsletter_height)];
if (mySwitchNewsletter != nil) {

    [varCommerceSettingsView addSubview:mySwitchNewsletter];


    float mySwitchScaleFactor = (varSettingsSwitchNewsletter_scale / 100.0);


    CGFloat dX=mySwitchNewsletter.bounds.size.width/2, dY=mySwitchNewsletter.bounds.size.height/2;
    mySwitchNewsletter.transform = CGAffineTransformTranslate(CGAffineTransformScale(CGAffineTransformMakeTranslation(-dX, -dY), mySwitchScaleFactor, mySwitchScaleFactor), dX, dY);

    mySwitchNewsletter release];
}

Where varSettingsSwitchNewsletter_scale is an int from 0 to 100 (%).

Al-Noor Ladhani
  • 2,413
  • 1
  • 22
  • 14
0
// Just in case someone trying to hard code UISwitch in Xcode 6.4 the following is working
// in .h
@property UISwitch * onoff;

// in .m

self.onoff = [[UISwitch alloc] initWithFrame:CGRectMake(160, 40, 0, 0)];
_onoff.transform = CGAffineTransformMakeScale(0.50, 0.50);
[self.view addSubview:self.onoff];
Aruna
  • 129
  • 1
  • 5