简体   繁体   English

如何在UIImageView的左上角和右下角创建UIButton

[英]How to create UIButton in UIImageView's top left corner and bottom right corner

I wish to create delete button in imageview's top left corner and bottom right corner. 我希望在imageview的左上角和右下角创建删除按钮。 but it doesn't look like what I needed. 但看起来不像我所需要的。

在此处输入图片说明

I wish both the buttons should be placed on corner of the Red border 我希望两个按钮都应该放在红色边框的一角

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];

please guide me. 请指导我。

I want like this 我想要这样 在此处输入图片说明

Just play around with the frame of the button: eg 只需按一下按钮的框架即可:例如

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

and

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

as the first 2 numbers are co-ordinates x and y and the frame is relative to the containing views frame. 因为前两个数字是x和y坐标,并且框架相对于包含的视图框架。

Just use zoomBtn.center rather than zoomBtn.frame - that way, you don't have to account for the size of the button - it would work for any size button. 只需使用zoomBtn.center而不是zoomBtn.frame-这样,您就不必考虑按钮的大小了-它适用于任何大小的按钮。

// Create the button's frame - doesn't matter the x & y
CGRect btnFrame = CGRectMake(0.0f, 0.0f, 20.0f, 20.0f);

zoomBtn.frame = btnFrame;

// Set the zoomBtn center to the bottom right corner
zoomBtn.center = CGPointMake(tappedView.frame.size.width, tappedView.frame.size.height);

deleteBtn.frame = btnFrame;

// Set the deleteBtn center to the top left corner
deleteBtn.center = CGPointMake(0.0f, 0.0f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅创建具有左上角和左下角半径的圆角UIButton - How to create rounded UIButton with top-left & bottom-left corner radius only 从UIImageView的左下角而不是右下角进行页面卷曲动画 - Page curl animation from left bottom corner instead of right bottom corner on UIImageView 如何为 UIView 的左下角、右下角和左上角设置 cornerRadius? - how to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView? UIImageView作为callout的leftCalloutAccessoryView自动切换左上角 - UIImageView as callout's leftCalloutAccessoryView automatically shifts top-left corner 如何在 UIImageView 中实现底部自定义曲线和顶部角半径? - How to Achieve bottom custom Curve and top Corner Radius in UIImageView? 自动调整大小蒙版-固定在UIImageView的左上角 - Autoresize mask - pin top left corner of UIImageView 拐角半径仅适用于视图的左上角和左下角 - Corner radius only for top and bottom left corner of a view 图像角半径仅左下角 - image corner radius only bottom left and right UITableviewCell底部左右角半径 - UITableviewCell Bottom Left & Right corner radius 如何在 UIView 中为左上角和右上角的圆角半径仅设置顶部、左侧和右侧的边框? - how to set boarder for only top, left and right with corner radius with top-left and top-right in UIView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM