2012-09-21 73 views
2

我希望在imageview的左上角和右下角創建刪除按鈕。但它看起來不像我所需要的。如何在UIImageView的左上角和右下角創建UIButton

enter image description here

我希望這兩個按鈕應放置在紅色邊框

的角球製造我用下面

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

代碼按鈕請指導我。

我想這樣enter image description here

回答

1

剛玩的按鈕的框架:如

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和框架是相對於包含視圖幀。

+0

是的。如果我將(-5,-5,20,20)放置在確切的位置,但在按鈕上出現紅色邊框,我已經嘗試過這一個。 – thavasidurai

+0

@Brendt我應該刪除我的帖子?兩個聲音都一樣.... – Spynet

+0

@Brendt我編輯了我的問題請看它。 – thavasidurai

0

只需使用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); 
相關問題