057 Adjust the edge spacing of the buttons on the screen

Source: Internet
Author: User

The effect is as follows:

ViewController.h

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

Viewcontroller.m

1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4- (void) Disablestateswitch: (Uiswitch *) sender;5- (void) Buttondidpush;6 @end7 8 @implementationViewcontroller9 Ten- (void) Viewdidload { One [Super Viewdidload]; ASelf.view.backgroundColor =[Uicolor Whitecolor]; -Self.navigationItem.title =@"Adjust the edge spacing of buttons on the screen"; -Uiswitch *swtdisablestate =[[Uiswitch alloc] init]; theSwtdisablestate.on =YES; - [swtdisablestate addtarget:self Action: @selector (Disablestateswitch:) forControlEvents: Uicontroleventvaluechanged]; -Uibarbuttonitem *barbtndisablestate =[[Uibarbuttonitem alloc] initwithcustomview:swtdisablestate]; - [self settoolbaritems:@[barbtndisablestate] animated:yes]; +      -_btntitleedgeinsets =[UIButton Buttonwithtype:uibuttontypecustom]; +_btntitleedgeinsets.frame = CGRectMake (0,0, the, -); A_btntitleedgeinsets.center =Self.view.center; at_btntitleedgeinsets.backgroundcolor =[Uicolor Whitecolor]; -_btntitleedgeinsets.layer.maskstobounds =YES; -_btntitleedgeinsets.layer.cornerradius =8.0; -     //Set Button caption font and shadow color, line Count -_btntitleedgeinsets.titlelabel.font = [Uifont boldsystemfontofsize: -]; -_btntitleedgeinsets.titlelabel.shadowoffset = Cgsizemake (2.0,2.0); in_btntitleedgeinsets.titlelabel.numberoflines =2; -     //set display characteristics in normal state to[_btntitleedgeinsets Settitle:@"Click I do state picture toggle + and adjust the edge spacing"Forstate:uicontrolstatenormal]; - [_btntitleedgeinsets Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; the [_btntitleedgeinsets Settitleshadowcolor:[uicolor Graycolor] forstate:uicontrolstatenormal]; *      $     //add a picture for each state of the buttonPanax Notoginseng[_btntitleedgeinsets setimage:[uiimage imagenamed:@"Dognormal"] forstate:uicontrolstatenormal]; -[_btntitleedgeinsets setimage:[uiimage imagenamed:@"doghighlighted"] forstate:uicontrolstatehighlighted]; the[_btntitleedgeinsets setimage:[uiimage imagenamed:@"dogdisabled"] forstate:uicontrolstatedisabled]; +     //set background picture in normal state AUIImage *imgstretchable = [[UIImage imagenamed:@"Frame"] Stretchableimagewithleftcapwidth: -Topcapheight: -]; the [_btntitleedgeinsets setbackgroundimage:imgstretchable forstate:uicontrolstatenormal]; +      -     //set the internal edge spacing of a button $ uiedgeinsets edgeinsets; $Edgeinsets.left = Edgeinsets.right = Edgeinsets.top = Edgeinsets.bottom =0; -_btntitleedgeinsets.titleedgeinsets =edgeinsets; - [_btntitleedgeinsets addtarget:self Action: @selector (Buttondidpush) forControlEvents: UIControlEventTouchUpInside]; the      - [Self.view addsubview:_btntitleedgeinsets];Wuyi } the  -- (void) didreceivememorywarning { Wu [Super didreceivememorywarning]; -     //Dispose of any resources the can be recreated. About } $  -- (void) Viewwillappear: (BOOL) Animated { - [Super viewwillappear:animated]; - [Self.navigationcontroller setnavigationbarhidden:no animated:animated]; A [Self.navigationcontroller settoolbarhidden:no animated:animated]; + } the  -- (void) Disablestateswitch: (Uiswitch *) Sender { $_btntitleedgeinsets.enabled =Sender.on; the } the  the- (void) Buttondidpush { the uiedgeinsets edgeinsets; -Edgeinsets.left =-80.0; inEdgeinsets.right = Edgeinsets.top = Edgeinsets.bottom =0; the     if(_btntitleedgeinsets.contentedgeinsets.left = =edgeinsets.left) { theEdgeinsets.left =60.0; About     } the_btntitleedgeinsets.contentedgeinsets =edgeinsets; the      the     if(_btntitleedgeinsets.titleedgeinsets.left = =0) { +Edgeinsets.left =30.0; -_btntitleedgeinsets.titleedgeinsets =edgeinsets; the     }Bayi } the  the @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

057 Adjust the edge spacing of the buttons on the screen

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.