056 Adding an image to the button

Source: Internet
Author: User

The effect is as follows:

ViewController.h

1 #import <UIKit/UIKit.h>23@interface  viewcontroller: Uiviewcontroller4 @property (Strong, Nonatomic) UIButton *Btnshadow; 5 6 @end

Viewcontroller.m

1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4- (void) Disablestateswitch: (Uiswitch *) sender;5 @end6 7 @implementationViewcontroller8 9- (void) Viewdidload {Ten [Super Viewdidload]; OneSelf.view.backgroundColor =[Uicolor Whitecolor]; ASelf.navigationItem.title =@"add an image to a button"; -Uiswitch *swtdisablestate =[[Uiswitch alloc] init]; -Swtdisablestate.on =YES; the [swtdisablestate addtarget:self Action: @selector (Disablestateswitch:) forControlEvents: Uicontroleventvaluechanged]; -Uibarbuttonitem *barbtndisablestate =[[Uibarbuttonitem alloc] initwithcustomview:swtdisablestate]; - [self settoolbaritems:@[barbtndisablestate] animated:yes]; -      +_btnshadow =[UIButton Buttonwithtype:uibuttontypecustom]; -_btnshadow.frame = CGRectMake (0,0,260, -); +_btnshadow.center =Self.view.center; A_btnshadow.backgroundcolor =[Uicolor Whitecolor]; at_btnshadow.layer.maskstobounds =YES; -_btnshadow.layer.cornerradius =8.0; -     //Set Button caption font and shadow color -_btnshadow.titlelabel.font = [Uifont boldsystemfontofsize: -]; -_btnshadow.titlelabel.shadowoffset = Cgsizemake (2.0,2.0); -     //set display characteristics in normal state in[_btnshadow Settitle:@"Click I do state picture toggle"Forstate:uicontrolstatenormal]; - [_btnshadow Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; to [_btnshadow Settitleshadowcolor:[uicolor Graycolor] forstate:uicontrolstatenormal]; +      -     //add a picture for each state of the button the[_btnshadow setimage:[uiimage imagenamed:@"Dognormal"] forstate:uicontrolstatenormal]; *[_btnshadow setimage:[uiimage imagenamed:@"doghighlighted"] forstate:uicontrolstatehighlighted]; $[_btnshadow setimage:[uiimage imagenamed:@"dogdisabled"] forstate:uicontrolstatedisabled];Panax Notoginseng     //set background picture in normal state -UIImage *imgstretchable = [[UIImage imagenamed:@"Frame"] Stretchableimagewithleftcapwidth: -Topcapheight: -]; the [_btnshadow setbackgroundimage:imgstretchable forstate:uicontrolstatenormal]; +      A [Self.view Addsubview:_btnshadow]; the } +  -- (void) didreceivememorywarning { $ [Super didreceivememorywarning]; $     //Dispose of any resources the can be recreated. - } -  the- (void) Viewwillappear: (BOOL) Animated { - [Super viewwillappear:animated];Wuyi [Self.navigationcontroller setnavigationbarhidden:no animated:animated]; the [Self.navigationcontroller settoolbarhidden:no animated:animated]; - } Wu  -- (void) Disablestateswitch: (Uiswitch *) Sender { About_btnshadow.enabled =Sender.on; $ } -  - @end

AppDelegate.h

1 #import <UIKit/UIKit.h>23@interface Appdelegate:uiresponder < Uiapplicationdelegate>4 @property (Strong, Nonatomic) UIWindow *window; 5 @property (Strong, Nonatomic) Uinavigationcontroller *Navigationcontroller; 6 7 @end

Appdelegate.m

1 #import "AppDelegate.h"2 #import "ViewController.h"3 4 @interfaceappdelegate ()5 @end6 7 @implementationappdelegate8 9-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {Ten_window =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; OneViewcontroller *viewcontroller =[[Viewcontroller alloc] init]; A_navigationcontroller =[[Uinavigationcontroller alloc] initwithrootviewcontroller:viewcontroller]; -_window.rootviewcontroller =_navigationcontroller; - [_window Addsubview:_navigationcontroller.view]; the [_window makekeyandvisible]; -     returnYES; - } -  +- (void) Applicationwillresignactive: (UIApplication *) Application { - } +  A- (void) Applicationdidenterbackground: (UIApplication *) Application { at } -  -- (void) Applicationwillenterforeground: (UIApplication *) Application { - } -  -- (void) Applicationdidbecomeactive: (UIApplication *) Application { in } -  to- (void) Applicationwillterminate: (UIApplication *) Application { + } -  the @end

056 Adding an image to the button

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.