首页 > 解决方案 > UIButton images for iPad

问题描述

I need bigger button & image sizes for iPad devices compared to iPhone. Before Size classes, I used to have images ending as @2x~ipad to have different version for iPad. However, it is not clear if the same approach is applicable when using Size classes and Trait collections, or there is a better approach? Also, is iPad Pro 12.9 inch @3x or @2x? I see conflicting responses, but if it is @2x, how do we have bigger image size for iPad Pro?

标签: iosipaduikitios-autolayoutsize-classes

解决方案


您将大小与分辨率混淆了。

如果你有一个UIImageView150 x 150 points,并且你有MyPic.png++ ,图像视图的框架将始终是MyPic@2x.pngpoints 但会自动为设备选择合适的分辨率图像。请参阅https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/image-size-and-resolution/MyPic@3x.png150 x 150 UIKit

如果您想要根据设备/视图大小为按钮和图像设置不同大小的框架,请使用特征变化、代码或基于百分比的约束。

同样,如果您有相同图像的MyPic.png++MyPic@2x.png文件,则无论大小MyPic@3x.png如何,都将使用适当的分辨率图像。

使用资产目录处理图像时有很大的灵活性。例如:

在此处输入图像描述

您可以为 1x 2x 和 3x 添加不同分辨率的图像...您可以添加特定于 iPhone / iPad / 等的图像集。


编辑

附加示例。资产被命名为“RoundSwift256x256”。iPhone 有 3 种分辨率,iPad 有 2 种分辨率(没有 iPad 使用 @3x)——使用不同的颜色使其非常明显。

在此处输入图像描述

在 Storyboard 中,我添加了一个 imageView,将其图像设置为RoundSwift256x256. 然后将助理编辑器设置为预览并选择 iPhone 8 和 iPad Pro 9.7"。如您所见,它使用我的 iPhone 图像为 iPhone 和我的 iPad 图像为 iPad。

在此处输入图像描述


推荐阅读