Http://www.cnblogs.com/ygm900/archive/2013/05/28/3104169.html
UIButton *btntemp = [Uibuttonbuttonwithtype:uibuttontypecustom];
Deep Custom One button
UIButton *btn1 = [UIButton buttonwithtype:uibuttontypecustom]; Btn1.frame = CGRectMake (0, 0, $, +); Btn1.center = Cgpointmake (215); Btn1.autoresizingmask = Uiviewautoresizingflexibletopmargin | Uiviewautoresizingflexibleleftmargin | Uiviewautoresizingflexiblerightmargin; [Btn1 settitle:@ "" forstate:uicontrolstatenormal]; [Btn1 addtarget:self Action: @selector (btntest) forcontrolevents:uicontroleventtouchupinside]; [Self.view ADDSUBVIEW:BTN1];
Note: buttons created with [UIButton Buttonwithtype:uibuttontypecustom] cannot be manually release and will be handed to the system. Cannot write [button release];
Questions & Discoveries
Two different ways of creating one button can display Titile, but the other cannot be displayed. as shown below;
UIButton *button = [UIButton buttonwithtype:uibuttontyperoundedrect]; Using this method of creation, button cannot display titile// button.frame = CGRectMake (screen_width*schemecount) +30+100, 20+ (j*30), 130, 40);
Created using Alloc, the button can display the title UIButton *button = [[UIButton alloc]initwithframe:cgrectmake (screen_width* Schemecount) +30+100, 20+ (j*30), (+)]; [Button settitle:@ "1" forstate:uicontrolstatenormal]; [Button Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; [Button Setbackgroundcolor:[uicolor Whitecolor]; [Self.uisv_bottomview Addsubview:button]; [Button release];
The reason for finding dog blood! When creating a button using the following method, [button release] cannot be used; Otherwise, the button cannot display the caption
Button.frame = CGRectMake (screen_width*schemecount) +30+100, 20+ (j*30), 130, 40);
question: After the practice found that when a button attached to a ImageView, button buttons do not press down the effect, and, can not set the background picture. Only color can be set. The following code, in [ImageView Addsubview:button]; does not work. Why? Can the button only add to the view? Instead of ImageView? (Is it also because of the dog blood release AH)
[Button setbackgroundimage:[uiimage imagenamed:@ "Button_anpai.png"] forstate:uicontrolstatenormal];
Set the text display position of the UIButton, font size, font color
Btn.frame = CGRectMake (x, y, width, height);
[BTN Settitle: @ "search" forstate:uicontrolstatenormal];
Set the size of the self on the button
[Btn SetFont: [Uifont systemfontsize:14.0]]; This can be used to set the size of the font, but may be removed in the future SDK version of the change method
should use
Btn.titleLabel.font = [Uifont systemfontofsize:14.0];
[Btn sebackgroundcolor: [Uicolor Bluecolor]];
Finally joins the button to the specified view Superview
[Superview ADDSUBVIEW:BTN]; Why do you want to assign to Superview?
==========================================================
Tvnamelabel=[[uibutton Alloc]initwithframe:cgrectmake (5,5,200,40)];
This initializes the button, the text default color is white, all if the background is also white, can not see the text,
Btn.contenthorizontalalignment=uicontrolcontenthorizontalalignmentleft;//Set text position, now set to left, default is center
But the problem comes out again, when the text is pressed to make the border, we can set
Btn.contentedgeinsets = Uiedgeinsetsmake (0,10, 0, 0);
Keeps the text distance of 10 pixels from the border.
=======================================================
Set the color of the font on UIButton UIButton the color of the font, not with:
[Btn.titlelabel Settextcolor:[uicolorblackcolor]];
Btn.titlelabel.textcolor=[uicolor Redcolor];
Instead, use:
[Btn Settitlecolor:[uicolor Blackcolor]forstate:uicontrolstatenormal];
Long Press button response event
{//Long press DELETE button Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc]initwithtarget:self Action: @selector (deletelongpressed)];/ / longpress.minimumpressduration = 0.5; The shortest pressing time, generally, this sentence does not write can also [Uib_delete addgesturerecognizer:longpress]; } Long press the Delete button to delete all entered numbers-(void) deletelongpressed{ uil_phonenumber.text = @ ""; Nsms_phonenumber = [[nsmutablestring alloc]initwithformat:@ "];}
UIButton two different ways to set the background image
First: Setbackgroudimage: Picture is stretched
The second type: setimage: The image remains the original size
Reference: http://my.oschina.net/joanfen/blog/160843 tells the picture to be stretched to set the background of the button.
The non-alloc button is not released, otherwise the button is not displayed. Although the button's Retaincount is still 1. As follows
Button Episode button = [UIButton buttonwithtype:uibuttontypecustom]; Button.frame = CGRectMake (0, K_headimageview_height, screen_width/2-1, 84/2-1); [Button Setbackgroundimage:[imageutilities Createimagewithcolor:[uicolor Whitecolor]] forstate: UIControlStateNormal]; [Button setbackgroundimage:[imageutilities createimagewithcolor:[colorutils colorwithhexstring:k_buttonselect_ Backgroudcolor]] forstate:uicontrolstateselected]; [Button settitle:@ "episode" Forstate:uicontrolstatenormal]; [Button.titlelabel Setfont:[uifont systemfontofsize:k_introduceinfo_fontsize]; [Button Settitlecolor:[colorutils Colorwithhexstring:k_introduceinfo_textcolor] forstate:uicontrolstatenormal]; [Button Settitlecolor:[colorutils Colorwithhexstring:k_buttonselect_textcolor] forstate:uicontrolstateselected]; [Button addtarget:self action: @selector (btnepisodeclicked:) forcontrolevents:uicontroleventtouchupinside]; Episodebutton = button; [Self.view Addsubview:episodebutton]; [Button release]; If you add release here, the button will not show up.
How to set up can let UIButton point, keep the state of press down, then click to Bounce?
Methods: Make a judgment to modify selected properties such as: [_tmpbtn Setselected:yes]; or [_tmpbtn Setselected:no];
UIButton's Object button has three states, the decibel is normal selected highlighted, what is the difference between them?
UIControlStateNormal refers to the normal state of the button, there is no objection to it.
Uicontrolstatehighlighted refers to the state when the button is pressed down!
Uicontrolstateselected refers to the state of the button when it is selected!
What's the difference between "press down" and "check"? "State when pressed" refers to the state when pressed by a finger. "Status when checked" means that the finger press is finished and left. The state of the button rendering. So, when you set a button to be pressed down when the picture changes, use uicontrolstatehighlighted instead of uicontrolstateselected.
Reference: Summary of the highlighted status of UIButton
UIButton a small trap-note: Titlelabel is readonly!
When using UIButton, it is sometimes necessary to dynamically change the text and style of the button at run time. But the iOS SDK documentation is not very clear, the use of the wrong API often produces bizarre results, such as clicking UIButton, text changes back to the default value.
This problem I met three back, every time I have to re-Google a bit to solve. Previously felt that these small problems, not worth writing an article alone, but often these problems are now common. In the future, we will continue to share some of the small problems encountered as tips, and hope that they can help other developers.
The wrong way
12 |
/* Wrong-*/yourbutton. Titlelabel. Text = @"Sampletext"; |
Here Titlelabel is readonly, but we can change the property of Titlelabel. The problem is that there are Default, Highlighted, Selected, Disabled several states in UIButton. This changes the text of the current state, but once the state changes (such as tap), it becomes the default setting.
The right way
12 |
/* Right-*/[yourbutton settitle:@ "Sampletext" forstate:uicontrolstatenormal]; |
If other states do not have special settings, setting UIControlStateNormal This state will automatically set the other state.
Reference: http://www.iwangke.me/2012/09/05/caveat-for-uibutton/
Let the UIButton not be highlighted when pressed
Method One:
button1.adjustsimagewhenhighlighted = NO; Cancel the button highlighting state (sometimes we don't want the system to give that state)
Reference: http://www.cocoachina.com/bbs/simple/?t106229.html
Method Two: Set a transparent picture for all states of UIButton. For more information, see: No highlighting when UIButton is pressed
Set the highlight glow effect of a button by a point
[CancelButton Setshowstouchwhenhighlighted:yes];
button to expand the touch response area
The original code is as follows: Menubtn.frame = CGRectMake (4, 8, 44, 28); Expand the touch area code as follows: [Menubtn setimage:[pyutiles imagefromfile:@ "Menubtn.png"] Forstate:uicontrolstatenormal]; [Menubtn setframe:cgrectmake (0, 0, 48, 44)]; [Menubtn setcontentmode:uiviewcontentmodecenter]; principle, expand the button's frame rect, and set the picture to be centered, note that if this side setimage, SetBackgroundImage will enlarge the image to the entire response area.
Reference: http://www.myexception.cn/mobile/550219.html
set font shadow for UIButton
[[Rightbutton Titlelabel] Setshadowcolor:[uicolor Blackcolor]]; [[Rightbutton Titlelabel] Setshadowoffset:cgsizemake (-0.5,-0.5)];
Adjust the margin (Padding) of the content inside the button
[Self.usernamebutton setcontentedgeinsets:uiedgeinsetsmake (0, 3, 0, 0)];
Reference: http://blog.csdn.net/ysy441088327/article/details/7660183
Keep UIButton highlighted (click to enter)
Reference: Summary of the highlighted status of the UIButton (the highlighted state of the button will be clear when it is up)
About UIButton Select and highlight picture transitions (click to enter)
Summary: When the state of the button is selected, the corresponding state of the highlight should be selected|highlighted, which is different from the highlighted state highlighted under normal.
The code is as follows:
[MyButton setimage:imageselectedhover forstate: (uicontrolstateselected | uicontrolstatehighlighted)];
The difference between Uibuttontypecustom and Uibuttontyperoundedrect
Uibuttontypecustom: is a button with a transparent background. If you want to do a button in the picture switch (different status), you must use two pictures to set.
Uibuttontyperoundedrect: is a rounded rectangle button. Convenience: The system automatically sets the highlight state (semitransparent state) for this button. That is, through a picture, you can make the effect of button interaction. Inconvenient place: If you want to customize the background of the button display in different states, this type of button is not supported well.
Recommendation: Rapid development, each button only one picture, it is recommended to use Uibuttontyperoundedrect. button height customization, it is recommended to use Uibuttontypecustom.
Use of Titleedgeinsets imageedgeinsets
[MyButton settitleedgeinsets:uiedgeinsetsmake (0, 0, 0, 0)]; The 4 parameters are the upper boundary, the left boundary, the lower boundary, and the right boundary.
Pits when using UIButton and Uiimageview
-"using UIButton to be transparent and then using UIImage or Uiimageview as the parent view of UIButton may cause the uimage area covered by UIButton to lose the event response." Solution: Set UIImage or uiimageview the human-Computer interaction property to Yes. . userinteractionenabled = YES;
2, in the same vein, if a imageview is assigned to UIButton as a UIButton child view, then the area covered by UIButton will lose its ability to respond to events. Solution: Let UIButton on the upper level, imageview on the lower level. Note that it is useless to set the human-Computer interaction property here.
UIButton How to identify yourself after adding long press events Uibutton.tagUIButton *btn = (uibutton*) Gesture.view; NSLog (@ "%d", Btn.tag);
Deep Custom One button