Changing UISwitch width and height

I tested the theory and it appears that you can use a scale transform to increase the size of the UISwitch UISwitch *aSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(120, 120, 51, 31)]; aSwitch.transform = CGAffineTransformMakeScale(2.0, 2.0); [self.view addSubview:aSwitch];

How to know the UITableview row number

Tags, subclasses, or view hierarchy navigation are too much work!. Do this in your action method: CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint]; Works with any type of view, multi section tables, whatever you can throw at it – as long as the origin of your sender is within the cell’s … Read more

Swift/UISwitch: how to implement a delegate/listener

UISwitch has no delegate protocol. You can listen to the status as follows: ObjC: // somewhere in your setup: [self.mySwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; – (void)switchChanged:(UISwitch *)sender { // Do something BOOL value = sender.on; } Swift: mySwitch.addTarget(self, action: “switchChanged:”, forControlEvents: UIControlEvents.ValueChanged) func switchChanged(mySwitch: UISwitch) { let value = mySwitch.on // Do something } Swift3 : … Read more

Swift – How to set initial value for NSUserDefaults

Swift 3 syntax example Register a boolean default value: UserDefaults.standard.register(defaults: [“SoundActive” : true]) And to get the value: UserDefaults.standard.bool(forKey: “SoundActive”) Sidenote: Although the above code will return true, note that the value isn’t actually written to disk until you set it: UserDefaults.standard.set(true, forKey: “SoundActive”)

UISwitch in a UITableView cell

Setting it as the accessoryView is usually the way to go. You can set it up in tableView:cellForRowAtIndexPath: You may want to use target/action to do something when the switch is flipped. Like so: – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { switch( [indexPath row] ) { case MY_SWITCH_CELL: { UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:@”SwitchCell”]; if( … Read more