【发布时间】:2015-04-04 04:54:25
【问题描述】:
我在尝试正确对齐 UIImageView 时遇到困难。
现在,我有一个非常简单的故事板,在一个视图中只有一个 UIButton 和 UIImageView(参见附图)。
控制器中已设置插座:
@property (weak, nonatomic) IBOutlet UIImageView *imageViewer;
对于UIImageView,查看模式设置为Aspect Fit。 UIImageView 的边缘与ViewController 的边缘对齐。当UIButton被点击时,会触发更新UIImageView图片:
@synthesize imageViewer;
- (IBAction)submitPressed:(id)sender {
... //url declared in this hidden chunk
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
imageViewer.image = [UIImage imageWithData:imageData];
}
如您所见,由于某种原因,左侧有一个边距,尽管 UIImageView 已设置为 Aspect Fit,但图像的右边缘发生了裁剪(红色部分已被裁剪掉)。
我尝试在所有边上放置正常的边距,图像在右侧被裁剪得更多。我认为这可能是NSData 的问题,并尝试将远程图像直接加载到UIWebView 中,但问题仍然存在。
有人知道为什么会这样吗?提前谢谢你。
【问题讨论】:
-
你在使用自动布局吗?
-
你尝试过使用约束吗?应用约束 Leading Space to Superview 和 Trailing Space to Superview 并将常量设置为 0
-
嗯,我没有看到这些约束选项。让我再看看
-
在您的 NIB 中选择 UIImageView,然后点击 Editor > Pin > Leading Space to Superview。然后在 Attribute Inspector 中将 Constant 设置为 0。执行相同操作并将 Training Space 应用于 SuperView。
-
成功了。感谢您的帮助!
标签: ios objective-c uiimageview