Monday, 26 August 2013

Introduction to UIButton

Programmatically create a UIButton (Basic Button)


 UIButton *sampleButton = [UIButton  buttonWithType:UIButtonTypeRoundedRect];

[sampleButton addTarget:self action:@selector(methodeName:)
 forControlEvents:UIControlEventTouchDown];
// Connect UIButton with a method "methodeName" So once user click button this methode will get called

[sampleButton setTitle:@"My Button" forState:UIControlStateNormal]; 

// Assign a name for the UIButton in Normal state ,other options are highlighted state and selected state


sampleButton.frame = CGRectMake(50,50,100,50);

// Create a frame for the UIButton , here first 50 indicate x-axis,second 50 indicate y-axis,100 indicates width of UIButton,and last 50 indicate height of UIButton

[self.view addSubview:sampleButton];

// Adding that button to view



Programmatically create a UIButton (Advanced Button)


UIButton *sampleButton = [UIButton buttonWithType: UIButtonTypeCustom];
sampleButton.frame = CGRectMake(50,50,100,50);
[sampleButton setBackgroundImage:sampleImage forState: UIControlStateNormal];
[sampleButton addTarget:self action:@selector(myButtonClick:)    forControlEvents:UIControlEventTouchUpInside]; 
sampleButton.adjustsImageWhenHighlighted = YES;
sampleButton.titleLabel.text = @"Click Me";
sampleButton.titleLabel.textColor = [UIColor redColor];
sampleButton.titleLabel.textAlignment = UITextAlignmentCenter;
sampleButton.titleLabel.font = [UIFont fontWithName: @"Helvetica" size: 14];
[self.view addSubview: sampleButton];

-(void) myButtonClick:(NSString *)myString{

   NSLog(@"Button clicked");
}


Here this line
[sampleButton setBackgroundImage:sampleImage forState: UIControlStateNormal];
is used to add an image as background to UIButton.

sampleImage=[UIImage imageNamed:"logo.png"]; 





Below contents are from Apple docs


Overview

An instance of the UIButton class implements a button on the touch screen. A button intercepts touch events and sends an action message to a target object when tapped. Methods for setting the target and action are inherited from UIControl. This class provides methods for setting the title, image, and other appearance properties of a button. By using these accessors, you can specify a different appearance for each button state.
For information about basic view behaviors, see View Programming Guide for iOS.

Tasks

Creating Buttons

Configuring the Button Title

Configuring Button Presentation

Configuring Edge Insets

Getting the Current State

Getting Dimensions

Deprecated Properties

Properties

adjustsImageWhenDisabled

A Boolean value that determines whether the image changes when the button is disabled.
@property(nonatomic) BOOL adjustsImageWhenDisabled
Discussion
If YES, the image is drawn darker when the button is disabled. The default value is YES.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

adjustsImageWhenHighlighted

A Boolean value that determines whether the image changes when the button is highlighted.
@property(nonatomic) BOOL adjustsImageWhenHighlighted
Discussion
If YES, the image is drawn lighter when the button is highlighted. The default value is YES.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

buttonType

The button type. (read-only)
@property(nonatomic, readonly) UIButtonType buttonType
Discussion
See UIButtonType for the possible values.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

contentEdgeInsets

The inset or outset margins for the rectangle surrounding all of the button’s content.
@property(nonatomic) UIEdgeInsets contentEdgeInsets
Discussion
Use this property to resize and reposition the effective drawing rectangle for the button content. The content comprises the button image and button title. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the UIEdgeInsetsMake function to construct a value for this property. The default value is UIEdgeInsetsZero.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

currentAttributedTitle

The current styled title that is displayed on the button. (read-only)
@property(nonatomic,readonly,retain) NSAttributedString *currentAttributedTitle
Discussion
The value for this property reflects the title associated with the control’s current state. For states that do not have a custom title string associated with them, this method returns the attributed title that is currently displayed, which is typically the one associated with the UIControlStateNormal state.
Availability
  • Available in iOS 6.0 and later.
Declared In
UIButton.h

currentBackgroundImage

The current background image displayed on the button. (read-only)
@property(nonatomic, readonly, retain) UIImage *currentBackgroundImage
Discussion
This value can be nil.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

currentImage

The current image displayed on the button. (read-only)
@property(nonatomic, readonly, retain) UIImage *currentImage
Discussion
This value can be nil.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

currentTitle

The current title that is displayed on the button. (read-only)
@property(nonatomic, readonly, retain) NSString *currentTitle
Discussion
The value for this property is set automatically whenever the button state changes. For states that do not have a custom title string associated with them, this method returns the title that is currently displayed, which is typically the one associated with the UIControlStateNormal state. The value may be nil.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

currentTitleColor

The color used to display the title. (read-only)
@property(nonatomic, readonly, retain) UIColor *currentTitleColor
Discussion
This value is guaranteed not to be nil. The default value is white.
Availability
  • Available in iOS 2.0 and later.
Related Sample Code
Declared In
UIButton.h

currentTitleShadowColor

The color of the title’s shadow. (read-only)
@property(nonatomic, readonly, retain) UIColor *currentTitleShadowColor
Discussion
The default value is white.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

imageEdgeInsets

The inset or outset margins for the rectangle around the button’s image.
@property(nonatomic) UIEdgeInsets imageEdgeInsets
Discussion
Use this property to resize and reposition the effective drawing rectangle for the button image. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the UIEdgeInsetsMake function to construct a value for this property. The default value is UIEdgeInsetsZero.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

imageView

The button’s image view. (read-only)
@property(nonatomic, readonly, retain) UIImageView *imageView
Discussion
Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance and behavior of the button’s view. For example:
UIButton *button                   = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.imageView.exclusiveTouch    = YES;
The imageView property returns a value even if the button has not been displayed yet. The value of the property is nil for system buttons.
Availability
  • Available in iOS 3.0 and later.
Declared In
UIButton.h

reversesTitleShadowWhenHighlighted

A Boolean value that determines whether the title shadow changes when the button is highlighted.
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted
Discussion
If YES, the shadow changes from engrave to emboss appearance when highlighted. The default value is NO.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

showsTouchWhenHighlighted

A Boolean value that determines whether tapping the button causes it to glow.
@property(nonatomic) BOOL showsTouchWhenHighlighted
Discussion
If YES, the button glows when tapped; otherwise, it does not. The image and button behavior is not changed by the glow. The default value is NO.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

tintColor

The tint color for the button.
@property(nonatomic, retain) UIColor *tintColor
Discussion
The default value is nil.
This property is not valid for all button types.
Availability
  • Available in iOS 5.0 and later.
Declared In
UIButton.h

titleEdgeInsets

The inset or outset margins for the rectangle around the button’s title text.
@property(nonatomic) UIEdgeInsets titleEdgeInsets
Discussion
Use this property to resize and reposition the effective drawing rectangle for the button title. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the UIEdgeInsetsMake function to construct a value for this property. The default value is UIEdgeInsetsZero.
The insets you specify are applied to the title rectangle after that rectangle has been sized to fit the button’s text. Thus, positive inset values may actually clip the title text.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

titleLabel

A view that displays the value of the currentTitle property for a button. (read-only)
@property(nonatomic, readonly, retain) UILabel *titleLabel
Discussion
Although this property is read-only, its own properties are read/write. Use these properties primarily to configure the text of the button. For example:
UIButton *button                  = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.titleLabel.font            = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode   = UILineBreakModeTailTruncation;
Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes.
The titleLabel property returns a value even if the button has not been displayed yet. The value of the property is nil for system buttons.
Availability
  • Available in iOS 3.0 and later.
Related Sample Code
Declared In
UIButton.h

Class Methods

buttonWithType:

Creates and returns a new button of the specified type.
+ (id)buttonWithType:(UIButtonType)buttonType
Parameters
buttonType
The button type. See UIButtonType for the possible values.
Return Value
A newly created button.
Discussion
This method is a convenience constructor for creating button objects with specific configurations. It you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.
When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

Instance Methods

attributedTitleForState:

Returns the styled title associated with the specified state.
- (NSAttributedString *)attributedTitleForState:(UIControlState)state
Parameters
state
The state that uses the styled title. The possible values are described in UIControlState.
Return Value
The title for the specified state. If no attributed title has been set for the specific state, this method returns the attributed title associated with the UIControlStateNormal state.
Availability
  • Available in iOS 6.0 and later.
Declared In
UIButton.h

backgroundImageForState:

Returns the background image used for a button state.
- (UIImage *)backgroundImageForState:(UIControlState)state
Parameters
state
The state that uses the background image. Possible values are described in UIControlState.
Return Value
The background image used for the specified state.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

backgroundRectForBounds:

Returns the rectangle in which the receiver draws its background.
- (CGRect)backgroundRectForBounds:(CGRect)bounds
Parameters
bounds
The bounding rectangle of the receiver.
Return Value
The bounds rectangle in which to draw any standard button content.
Discussion
The default implementation of this method returns the value in the bounds parameter. This rectangle represents the area in which the button draws its standard background content. Subclasses that provide custom background adornments can override this method and return a modified bounds rectangle to prevent the button from drawing over any custom content.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

contentRectForBounds:

Returns the rectangle in which the receiver draws its entire content.
- (CGRect)contentRectForBounds:(CGRect)bounds
Parameters
bounds
The bounding rectangle for the receiver.
Return Value
The rectangle in which the receiver draws its entire content.
Discussion
The content rectangle is the area needed to display the image and title including any padding and adjustments for alignment and other settings.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

imageForState:

Returns the image used for a button state.
- (UIImage *)imageForState:(UIControlState)state
Parameters
state
The state that uses the image. Possible values are described in UIControlState.
Return Value
The image used for the specified state.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

imageRectForContentRect:

Returns the rectangle in which the receiver draws its image.
- (CGRect)imageRectForContentRect:(CGRect)contentRect
Parameters
contentRect
The content rectangle for the receiver.
Return Value
The rectangle in which the receiver draws its image.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

setAttributedTitle:forState:

Sets the styled title to use for the specified state.
- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state
Parameters
title
The styled text string so use for the title.
state
The state that uses the specified title. The possible values are described in UIControlState.
Discussion
Use this method to set the title of the button, including any relevant formatting information. If you set both a title and an attributed title for the button, the button prefers the use of the attributed title.
At a minimum, you should set the value for the normal state. If a title is not specified for a state, the default behavior is to use the title associated with the UIControlStateNormal state. If the value for UIControlStateNormal is not set, then the property defaults to a system value.
Availability
  • Available in iOS 6.0 and later.
Declared In
UIButton.h

setBackgroundImage:forState:

Sets the background image to use for the specified button state.
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
Parameters
image
The background image to use for the specified state.
state
The state that uses the specified image. The values are described in UIControlState.
Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
Availability
  • Available in iOS 2.0 and later.
Related Sample Code
Declared In
UIButton.h

setImage:forState:

Sets the image to use for the specified state.
- (void)setImage:(UIImage *)image forState:(UIControlState)state
Parameters
image
The image to use for the specified state.
state
The state that uses the specified title. The values are described in UIControlState.
Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
Availability
  • Available in iOS 2.0 and later.
Related Sample Code
Declared In
UIButton.h

setTitle:forState:

Sets the title to use for the specified state.
- (void)setTitle:(NSString *)title forState:(UIControlState)state
Parameters
title
The title to use for the specified state.
state
The state that uses the specified title. The possible values are described in UIControlState.
Discussion
Use this method to set the title for the button. The title you specify derives its formatting from the button’s associated label object. If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one.
At a minimum, you should set the value for the normal state. If a title is not specified for a state, the default behavior is to use the title associated with the UIControlStateNormal state. If the value for UIControlStateNormal is not set, then the property defaults to a system value.
Availability
  • Available in iOS 2.0 and later.
Related Sample Code
Declared In
UIButton.h

setTitleColor:forState:

Sets the color of the title to use for the specified state.
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
Parameters
color
The color of the title to use for the specified state.
state
The state that uses the specified color. The possible values are described in UIControlState.
Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
Availability
  • Available in iOS 2.0 and later.
Related Sample Code
Declared In
UIButton.h

setTitleShadowColor:forState:

Sets the color of the title shadow to use for the specified state.
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state
Parameters
color
The color of the title shadow to use for the specified state.
state
The state that uses the specified color. The possible values are described in UIControlState.
Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

titleColorForState:

Returns the title color used for a state.
- (UIColor *)titleColorForState:(UIControlState)state
Parameters
state
The state that uses the title color. The possible values are described in UIControlState.
Return Value
The color of the title for the specified state.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

titleForState:

Returns the title associated with the specified state.
- (NSString *)titleForState:(UIControlState)state
Parameters
state
The state that uses the title. The possible values are described in UIControlState.
Return Value
The title for the specified state. If no title has been set for the specific state, this method returns the title associated with the UIControlStateNormal state.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

titleRectForContentRect:

Returns the rectangle in which the receiver draws its title.
- (CGRect)titleRectForContentRect:(CGRect)contentRect
Parameters
contentRect
The content rectangle for the receiver.
Return Value
The rectangle in which the receiver draws its title.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

titleShadowColorForState:

Returns the shadow color of the title used for a state.
- (UIColor *)titleShadowColorForState:(UIControlState)state
Parameters
state
The state that uses the title shadow color. The possible values are described in UIControlState.
Return Value
The color of the title’s shadow for the specified state.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

Constants

UIButtonType

Specifies the style of a button.
typedef enum {
   UIButtonTypeCustom = 0,
   UIButtonTypeRoundedRect,
   UIButtonTypeDetailDisclosure,
   UIButtonTypeInfoLight,
   UIButtonTypeInfoDark,
   UIButtonTypeContactAdd,
} UIButtonType;
Constants
UIButtonTypeCustom
No button style.
Available in iOS 2.0 and later.
Declared in UIButton.h.
UIButtonTypeRoundedRect
A rounded-rectangle style button.
Available in iOS 2.0 and later.
Declared in UIButton.h.
UIButtonTypeDetailDisclosure
A detail disclosure button.
Available in iOS 2.0 and later.
Declared in UIButton.h.
UIButtonTypeInfoLight
An information button that has a light background.
Available in iOS 2.0 and later.
Declared in UIButton.h.
UIButtonTypeInfoDark
An information button that has a dark background.
Available in iOS 2.0 and later.
Declared in UIButton.h.
UIButtonTypeContactAdd
A contact add button.
Available in iOS 2.0 and later.
Declared in UIButton.h.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIButton.h

No comments:

Post a Comment