我正在尝试创建一个从Web数据库中获取问题的测验应用。有些问题有图片,有些则没有。我希望图像占据屏幕的所有空闲区域(不改变方面无线电)。我无法确定图片的位置,因为问题文本的长度变化很大。 当我点击“开始游戏”时,我得到的第一个问题就像我希望它看起来一样(图片1),但是稍后我的视图似乎重新绘制了自己,图像将问题文本向下推。 (图2)
这是我用来更新视图的功能:
- (void) loadCurrentQuestion {
[loadDialog dismissWithClickedButtonIndex:-1 animated:NO];
self.navigationController.navigationBar.topItem.title = [[[@"Spurning " stringByAppendingString:[NSString stringWithFormat: @"%d", nr]] stringByAppendingString:@"/"] stringByAppendingString:[NSString stringWithFormat: @"%d", numOfQuestions]];
tapToContinue = NO;
//question.text = currQuestion.question;
question.text = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
//[image setNeedsDisplay];
//[question setNeedsDisplay];
image.contentMode = UIViewContentModeScaleAspectFit;
[c1 setTitle: [currQuestion getAnswer:1] forState:UIControlStateNormal];
c1.titleLabel.numberOfLines = 3;
c2.titleLabel.numberOfLines = 3;
c3.titleLabel.numberOfLines = 3;
c4.titleLabel.numberOfLines = 3;
[c2 setTitle: [currQuestion getAnswer:2] forState:UIControlStateNormal];
[c3 setTitle: [currQuestion getAnswer:3] forState:UIControlStateNormal];
[c4 setTitle: [currQuestion getAnswer:4] forState:UIControlStateNormal];
[c1 setEnabled:YES];
[c2 setEnabled:YES];
[c3 setEnabled:YES];
[c4 setEnabled:YES];
[c1 setBackgroundColor:[UIColor whiteColor]];
[c2 setBackgroundColor:[UIColor whiteColor]];
[c3 setBackgroundColor:[UIColor whiteColor]];
[c4 setBackgroundColor:[UIColor whiteColor]];
if (!(currQuestion.image == (id)[NSNull null]) ) {
if ([currQuestion.image length] > 1000)
image.image = [UIImage imageWithData: [[ImageProcessing alloc] stringToImage: currQuestion.image]];
else
image.image = [UIImage imageNamed:@"question_no_image.jpg"];
}
else
image.image = [UIImage imageNamed:@"question_no_image.jpg"];
//[loadDialog dismissWithClickedButtonIndex:-1 animated:NO];
}
我很确定我在Storyboard中使用了正确的约束,因为正确的外观出现在开头。 我该如何解决这个问题?
答案 0 :(得分:0)
To the below method send your image and the size for the image you need this will not spoil aspect ration and fix the image size in that view
+(UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect forImageViewRect:(CGSize)imageVwRect{
// UIImage *tempImage;
// tempImage=[self scaleImage:imageToCrop toSize:imageVwRect];
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// cropped=[self scaleImage:cropped toSize:rect.size];
return cropped;
}