代码之家  ›  专栏  ›  技术社区  ›  thavasidurai

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

  •  3
  • thavasidurai  · 技术社区  · 12 年前

    我希望在图像视图的左上角和右下角创建删除按钮。但它看起来不像我需要的。

    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

    2 回复  |  直到 12 年前
        1
  •  2
  •   Brendt    12 年前

    只要摆弄一下按钮的边框:例如。

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

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

    因为前两个数字是坐标x和y,并且帧是相对于包含视图帧的。

        2
  •  0
  •   siburb    11 年前

    只需使用zoomBtn.center而不是zoomBton.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);