如何在 UIImageView 的左上角和右下角创建 UIButton [英] How to create UIButton in UIImageView's top left corner and bottom right corner

查看:90
本文介绍了如何在 UIImageView 的左上角和右下角创建 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 imageview 的左上角和右下角创建删除按钮.但它看起来不像我需要的.

I wish to create delete button in imageview's top left corner and bottom right corner. but it doesn't look like what I needed.

我希望两个按钮都放在红色边框

为了创建按钮,我使用了下面的代码

To create the button I used the code below

   UIImageView * tappedView = (UIImageView *)[recognizer view];

[tappedView.layer setBorderColor: [[UIColor redColor] CGColor]];
[tappedView.layer setBorderWidth: 2.0];
tappedView.layer.cornerRadius = 10;
tappedView.layer.masksToBounds = NO;


UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame = CGRectMake(0, 0, 20, 20);

[deleteBtn setImage:[UIImage imageNamed:@"close.png"]forState:UIControlStateNormal];

deleteBtn.layer.shadowColor = [[UIColor blackColor] CGColor];
deleteBtn.layer.shadowOffset = CGSizeMake(0,4);
deleteBtn.layer.shadowOpacity = 0.3;
[tappedView addSubview:deleteBtn];
[deleteBtn addTarget:self action:@selector(deleteProperties:) forControlEvents:UIControlEventTouchUpInside];



UIButton *zoomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
zoomBtn.frame = CGRectMake(tappedView.frame.size.width, tappedView.frame.size.height, 20, 20);

[zoomBtn setImage:[UIImage imageNamed:@"close.png"]forState:UIControlStateNormal];

zoomBtn.layer.shadowColor = [[UIColor blackColor] CGColor];
zoomBtn.layer.shadowOffset = CGSizeMake(0,4);
zoomBtn.layer.shadowOpacity = 0.3;
[tappedView addSubview:zoomBtn];
[zoomBtn addTarget:self action:@selector(ZoomIn:) forControlEvents:UIControlEventTouchUpInside];

请指导我.

我想要这样

推荐答案

只是玩弄按钮的框架:例如

Just play around with the frame of the button: e.g.

deleteBtn.frame = CGRectMake(-5, -5, 20, 20);

zoomBtn.frame = CGRectMake(tappedView.frame.size.width - 20, tappedView.frame.size.height - 20, 20, 20);

因为前 2 个数字是坐标 x 和 y,并且框架是相对于包含视图框架的.

as the first 2 numbers are co-ordinates x and y and the frame is relative to the containing views frame.

这篇关于如何在 UIImageView 的左上角和右下角创建 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆