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

半腔热情 提交于 2019-12-24 03:18:15

问题


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.

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


回答1:


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

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.




回答2:


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.

// 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);


来源:https://stackoverflow.com/questions/12525512/how-to-create-uibutton-in-uiimageviews-top-left-corner-and-bottom-right-corner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!