josh
josh

Reputation: 1689

How to resize UISwitch button?

I want to resize my UISwitch button which is attach on UITableView. I found some help on google and did this successfully using CGAffineTransformMakeScale but 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 ViewDidLoad delegate. Here is what I am doing.

- (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.

Upvotes: 3

Views: 7367

Answers (3)

Sethmr
Sethmr

Reputation: 3084

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. SwiftySwitch It 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.

Upvotes: 0

henryaz
henryaz

Reputation: 880

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

@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.

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.

Upvotes: 2

Minakshi
Minakshi

Reputation: 1433

Try this

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

Upvotes: 5

Related Questions