iOS Development UI Chapter-add buttons and tag parameter descriptions in ImageView
First, the tag parameter
A view typically has only one parent view and multiple child views, which can be removed by using the child view's tag in development. The method is Viewwithtag:
Cue point: In Xib, if you want to get the corresponding control (property) through the tag parameter, do not set the tag parameter to 0, because all objects in Xib have a default tag of 0 and the object is set to 0.
Second,Add a button in ImageView
(1) Comparison of ImageView and buttons
You can place multiple pictures (4) Inside the button, and only one picture in ImageView.
(2) Description:
ImageView can only display one picture, and we know that all UI controls are inherited from UIView, and that all views are containers, and that it's easy to add something to the inside. Can you add a button to the ImageView?
(3) Action to add a button to the ImageView
There are usually two ways to create a control, one to drag directly on the storyboard or Xib interface designer, and another to create it in the form of handwritten code.
Dragging on the interface designer doesn't add a button to the ImageView, so let's try the handwritten code.
The code is as follows:
1#import"YYViewController.h"23@interfaceYyviewcontroller ()4@end56@implementationYyviewcontroller78-(void) Viewdidload9{10[Super Viewdidload];1112//Uiimageview user interaction is not allowed by defaultUiimageview *imageview = [[Uiimageview alloc] Initwithframe:cgrectmake (0,20,100,100)];14[Self.view Addsubview:imageview];Imageview.backgroundcolor =[Uicolor Redcolor];imageview.userinteractionenabled =YES;17UIButton *btn =[UIButton Buttonwithtype:uibuttontypecontactadd];19 [ImageView addsubview:btn];21 [btn addtarget:self action: @selector (click) Forcontrolevents:uicontroleventtouchupinside]; 22 }23 24-(void25 Span style= "color: #000000;" >{26 NSLog (@ " touch me "); 27 }28 @end
(4) Execution effect (after adding + button, click):
(5) attention points:
In the code above, imageview.userinteractionenabled = YES, the function is to set ImageView to allow the user to interact.
ImageView default is to not allow user interaction, this can be viewed by viewing the ImageView property sidebar in the interface designer.
Note the properties of the default state
Text top-top iOS Development UI-add buttons to Uiimageview and parameter descriptions for tags