Monday, 26 August 2013

Introduction to UISwitch

Why we are using UISwitch ??



Displays an element that shows the user the boolean state of a given value.  By tapping the control, the state can be toggled.


Output of the example code shown in below images 





Programmatically create a UISwitch


 UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(130, 235, 0, 0)];

[mySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
    
[self.view addSubview:mySwitch];

//  When user move the switch from OFF state to ON state the below methode is get called .

- (void)changeSwitch:(id)sender{
    
    if([sender isOn]){
        NSLog(@"Switch is ON");
    } else{
        NSLog(@"Switch is OFF");
    }
    
}













Below contents are from Apple Docs


Overview

You use the UISwitch class to create and manage the On/Off buttons you see, for example, in the preferences (Settings) for such services as Airplane Mode. These objects are known as switches.
The UISwitch class declares a property and a method to control its on/off state. As with UISlider, when the user manipulates the switch control (“flips” it) a UIControlEventValueChanged event is generated, which results in the control (if properly configured) sending an action message.
You can customize the appearance of the switch by changing the color used to tint the switch when it is in the on position.
For information about basic view behaviors, see View Programming Guide for iOS.

Tasks

Initializing the Switch Object

Setting the Off/On State

Customizing the Appearance of the Switch

Properties

offImage

The image displayed while the switch is in the off position.
@property(nonatomic, retain) UIImage *offImage
Discussion
This image represents the interior contents of the switch. The image you specify is composited with the switch’s rounded bezel and thumb to create the final appearance.
The size of this image must be less than or equal to 77 points wide and 27 points tall. If you specify larger images, the edges may be clipped.
Availability
  • Available in iOS 6.0 and later.
Declared In
UISwitch.h

on

A Boolean value that determines the off/on state of the switch.
@property(nonatomic, getter=isOn) BOOL on
Discussion
This property allows you to retrieve and set (without animation) a value determining whether the UISwitch object is on or off.
Availability
  • Available in iOS 2.0 and later.
Declared In
UISwitch.h

onImage

The image displayed when the switch is in the on position.
@property(nonatomic, retain) UIImage *onImage
Discussion
This image represents the interior contents of the switch. The image you specify is composited with the switch’s rounded bezel and thumb to create the final appearance.
The size of this image must be less than or equal to 77 points wide and 27 points tall. If you specify larger images, the edges may be clipped.
Availability
  • Available in iOS 6.0 and later.
Declared In
UISwitch.h

onTintColor

The color used to tint the appearance of the switch when it is turned on.
@property(nonatomic, retain) UIColor *onTintColor
Availability
  • Available in iOS 5.0 and later.
Declared In
UISwitch.h

thumbTintColor

The color used to tint the appearance of the thumb.
@property(nonatomic, retain) UIColor *thumbTintColor
Discussion
If the value of this property is nil, the tint color is derived from the value in the tintColor property.
Availability
  • Available in iOS 6.0 and later.
Declared In
UISwitch.h

tintColor

The color used to tint the appearance when the switch is disabled.
@property(nonatomic, retain) UIColor *tintColor
Discussion
If you do not specify a color for the thumbTintColor property, this property is also used to tint the thumb of the switch.
Availability
  • Available in iOS 6.0 and later.
Declared In
UISwitch.h

Instance Methods

initWithFrame:

Returns an initialized switch object.
- (id)initWithFrame:(CGRect)frame
Parameters
frame
A rectangle defining the frame of the UISwitch object. The size components of this rectangle are ignored.
Return Value
An initialized UISwitch object or nil if the object could not be initialized.
Discussion
UISwitch overrides initWithFrame: and enforces a size appropriate for the control.
Availability
  • Available in iOS 2.0 and later.
Declared In
UISwitch.h

setOn:animated:

Set the state of the switch to On or Off, optionally animating the transition.
- (void)setOn:(BOOL)on animated:(BOOL)animated
Parameters
on
YES if the switch should be turned to the On position; NO if it should be turned to the Off position. If the switch is already in the designated position, nothing happens.
animated
YES to animate the “flipping” of the switch; otherwise NO.
Discussion
Setting the switch to either position does not result in an action message being sent.
Availability
  • Available in iOS 2.0 and later.
Declared In
UISwitch.h

No comments:

Post a Comment