我目前正在使用此示例代码生成一个ImageView,其下方有一个按钮,按下时会生成一个日志。
- (void)loadView {
[super loadView];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SteveJobsMacbookAir.JPG"]];
[imageView setFrame:CGRectMake(80.0, 20.0, 160.0, 230.0)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[[self view] addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(cropImage) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(124.0, 258.0, 72.0, 37.0)];
[button setTitle:@"Crop!" forState:UIControlStateNormal];
[[self view] addSubview:button];
}
当我按下"裁剪!"按钮,上面图片的大小改变大小:
- (void)cropImage {
[imageView setFrame:CGRectMake(80.0, 20.0, 160.0, 230.0)];
}
但是,按钮保持在同一位置,而不是在图像下方。如何重新调整图像的框架,同时让按钮再次移动到其下方?
答案 0 :(得分:0)
只需将按钮的y位置设置为相对于imageView的y位置的变量。 试试这个
[button setFrame:CGRectMake(124.0, imageView.frame.size.height + imageView.frame.origin.y + 20, 72.0, 37.0)];
为imageView和按钮之间的某个空间提供了+20。 希望这会有所帮助:)
答案 1 :(得分:0)
使用以下代码
- (void)loadView {
[super loadView];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SteveJobsMacbookAir.JPG"]];
[imageView setFrame:CGRectMake(80.0, 20.0, 160.0, 230.0)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[[self view] addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//Change bellow line
[button addTarget:self action:@selector(cropImage:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(124.0, 258.0, 72.0, 37.0)];
[button setTitle:@"Crop!" forState:UIControlStateNormal];
[[self view] addSubview:button];
}
//Change to this function
- (void)cropImage:(UIButton *)sender {
[imageView setFrame:CGRectMake(80.0, 20.0, 160.0, 230.0)];
sender.center = CGPointMake(sender.center.x,imageView.center.y);
}