UIButton右上方添加一个笑button如:
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame= CGRectMake(100, 400, 120, 30);
[self.view addSubview:button];
UIButton * btn1 = [[UIButton alloc]initWithFrame:CGRectMake(button.frame.size.width-10, -10, 20, 20)];
[btn1 setImage:[UIImage imageNamed:@"ic_login_weixin"] forState:UIControlStateNormal];
[button addSubview:btn1];
button.layer.borderWidth = 1.f;
button.layer.borderColor = [UIColor blackColor].CGColor;
[btn1 addTarget:self action:@selector(touches) forControlEvents:UIControlEventTouchUpInside];虽然右上方的but1可以完全显示出来,但是点击btn1不能进行交互,也就是不能触发touches函数。
同理,在UITextFiled的右上方添加一个button如:
UITextField * filed = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 120, 30)];
[self.view addSubview:filed];
filed.borderStyle = UITextBorderStyleRoundedRect;
filed.layer.masksToBounds = NO;
UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(filed.frame.size.width-5, -5, 10, 10)];
[filed addSubview:btn];
btn.backgroundColor = [UIColor redColor];
self.view.backgroundColor = [UIColor whiteColor];
[btn addTarget:self action:@selector(touches) forControlEvents:UIControlEventTouchUpInside];
点击右上方的btn同样是不能进行交互的,也就是说该事件被filed给拦截了。
实验,将
filed.userInteractionEnabled = NO;
btn.userInteractionEnabled = YES;
同样也会产生冲突不能进行交互。
但是!!!注意!!!但是!!!
实验UIImageView上面添加btn,然后让imageView可以交互,就能完成右上方的小btn点击时间啦!!!
代码如下:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 400, 80, 80)];
UIButton* delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
delBtn.tag = 101;
delBtn.frame = CGRectMake(imageView.frame.size.width-20, -10, 30, 30);
[delBtn setImage:[UIImage imageNamed:@"ic_login_weixin"] forState:UIControlStateNormal];
[delBtn addTarget:self action:@selector(delBtntouches) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:delBtn];
[self.view addSubview:imageView];
imageView.image = [UIImage imageNamed:@"addimage"];
imageView.backgroundColor = [UIColor redColor];
UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewTouched)];
[imageView addGestureRecognizer:ges];
imageView.userInteractionEnabled= YES;
而且非常有意思的是,当点击除了delBtn的imageView的其它部分,响应的都是imageViewTouched,而点击了delBtn,则相应的是delBtntouches,但是delBtn超出imageview的部分点击也是没有效的。
由此我们可以推论出:uilabel已定也是可以这样来进行小btn的添加
最后:该理论的实际应用:
我们可以 在原来的图像上自定义来写一些图片增删的功能了。