简体   繁体   English

如何以像素为单位设置 UIVIew 的宽度和高度?

[英]how to set width and height of a UIVIew in pixels?

I am working on an ObjectiveC app in which I need to set the width and height of a uiview in pixels and than scale that uiview equal to the devices width.我正在开发一个 ObjectiveC 应用程序,其中我需要以像素为单位设置 uiview 的宽度和高度,然后将该 uiview 缩放为等于设备宽度。 The width and height values in pixels are fixed ( let say 900 px x 500 px).以像素为单位的宽度和高度值是固定的(比如 900 像素 x 500 像素)。

Currently I am doing this,目前我正在这样做,

    UIScreen* mainScreen = [UIScreen mainScreen];

    CGRect frame = CGRectMake(0, 0, 900.0, 500.0);
    [view setFrame:frame];
    CGFloat valScale = mainScreen.bounds.size.width/900.0;
    [view setContentScaleFactor:valScale];

But this is not giving me the desired values.但这并没有给我想要的值。

What should I do?我该怎么办?

(PS) Does view.frame.size.width return the width in pixels or points? (PS) view.frame.size.width是否以像素或点为单位返回宽度?

Hi for scaling a view you can use the transform property.嗨缩放视图,您可以使用转换属性。 Say you wish to scale your UIView by a factor of 2 then you can use.假设您希望将 UIView 缩放 2 倍,然后您可以使用。

self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);

CGRect width and height returns points not pixels. CGRect 宽度和高度返回点而不是像素。 One point has two pixels for 2x devices like iPhone 6,7 and three pixels for 3x devices like iPhone 6 Plus,7 Plus.一点对于 iPhone 6,7 等 2x 设备有两个像素,对于 iPhone 6 Plus,7 Plus 等 3x 设备有三个像素。

For more detail you can refer this Apple's documentation link.有关更多详细信息,您可以参考Apple 文档链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM