Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 853
josh Avatar asked Dec 11 '25 08:12

josh


2 Answers

Try this

 UISwitch *mySwitch = [UISwitch new];
mySwitch.transform = CGAffineTransformMakeScale(0.75, 0.75);
like image 154
Minakshi Avatar answered Dec 14 '25 05:12

Minakshi


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.

like image 42
henryaz Avatar answered Dec 14 '25 03:12

henryaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!