我试图在UIImageView的右上角以编程方式创建一个UIButton,到目前为止我有一个UIImageView,我想在图像加载到图像视图时在UIImageView的右上边缘创建一个UIButton。我不知道怎么能实现这一目标。
vector
[self.img1 addsubView:button];
这里,img1是图像视图。我想在img1的右上角添加该按钮。
我怎样才能做到这一点?
答案 0 :(得分:2)
[self.img1 addSubView:button];
[button setFrame:CGRectMake(self.img1.size.width-button.frame.size.width,0,button.frame.size.width, button.frame.size.height)];
答案 1 :(得分:0)
您也可以使用自动布局。
NSMutableArray * constraints = [NSMutableArray array];
[constraints addObject:[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeTopMargin
relatedBy:NSLayoutRelationEqual
toItem:separatorView
attribute:NSLayoutAttributeY
multiplier:1.0
constant:0]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeRightMargin
relatedBy:NSLayoutRelationEqual
toItem:separatorView
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0]];
[button addConstraints:constraints];
[self.img1 addsubView : button];
答案 2 :(得分:0)
UIImageView *img1 = [[UIImageView alloc] initWithFrame:CGRectMake(imageX,imageY,imageWidth,imageHeight)];
float buttonWidth = 30;
UIButton *button =[ [UIButton alloc] initWithFrame:CGRectMake(img1.frame.size.width - buttonWidth,0, buttonWidth,buttonHeight)];
[self.view addSubView:img1];
[img1 addSubView:button];