CGRectMake 适用于不同的 iphone ipad 方向

发布于 2024-11-16 16:21:24 字数 830 浏览 3 评论 0原文

对于以下代码,我需要根据 iphone 和 ipad 方向设置不同的 UIIMageView 位置。

// Animated images - centered on screen
animatedImages = [[UIImageView alloc] initWithFrame:CGRectMake(
(SCREEN_WIDTH / 2) - (IMAGE_WIDTH / 2), 
(SCREEN_HEIGHT / 2) - (IMAGE_HEIGHT / 2) + STATUS_BAR_HEIGHT,
IMAGE_WIDTH, IMAGE_HEIGHT)]; 

因为 CGRect 对于 ipad 纵向、iphone 纵向、ipad 横向和 iphone 横向会有所不同。

我该怎么做

if (Ipad Portrait orientation)
{ 
code with different position using CGRectMake (...............)
}
if (Iphone Portrait orientation)
{ 
code with different position using CGRectMake (...............)
}
if (Ipad Landscape orientation)
{ 
code with different position using CGRectMake (...............)
}
if (Iphone Landscape orientation)
{ 
code with different position using CGRectMake (...............)
}

For following code I need to set different UIIMageView Positions according to iphone and ipad orientation.

// Animated images - centered on screen
animatedImages = [[UIImageView alloc] initWithFrame:CGRectMake(
(SCREEN_WIDTH / 2) - (IMAGE_WIDTH / 2), 
(SCREEN_HEIGHT / 2) - (IMAGE_HEIGHT / 2) + STATUS_BAR_HEIGHT,
IMAGE_WIDTH, IMAGE_HEIGHT)]; 

As CGRect will be different for ipad portrait,iphone portrait, ipad landscape and iphone landscape.

How do I do this

if (Ipad Portrait orientation)
{ 
code with different position using CGRectMake (...............)
}
if (Iphone Portrait orientation)
{ 
code with different position using CGRectMake (...............)
}
if (Ipad Landscape orientation)
{ 
code with different position using CGRectMake (...............)
}
if (Iphone Landscape orientation)
{ 
code with different position using CGRectMake (...............)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮云落日 2024-11-23 16:21:24

您可以使用 UIViewController 的 interfaceOrientation 属性。它将为您提供以下值之一:

UIInterfaceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft,
UIInterfaceOrientationLandscapeRight

您的代码如下所示:

if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
{
    ...
}
else if    // etc.

You can use the UIViewController's interfaceOrientation property. It will give you one of the following values:

UIInterfaceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft,
UIInterfaceOrientationLandscapeRight

Your code would look like:

if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
{
    ...
}
else if    // etc.
情丝乱 2024-11-23 16:21:24

您可以使用 区分 iPhone 和 iPad

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
     // The device is an iPad running iPhone 3.2 or later.
}
else
{
     // The device is an iPhone or iPod touch.
}

,也可以使用 区分纵向和横向,

if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
     // The device is in landscape.
}
else
{
     // The device is in portrait.
}

现在将其结合起来,即可根据您的喜好进行自定义。

You can differentiate between iPhone and iPad with

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
     // The device is an iPad running iPhone 3.2 or later.
}
else
{
     // The device is an iPhone or iPod touch.
}

and you can differentiate between portrait and landscape with

if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
     // The device is in landscape.
}
else
{
     // The device is in portrait.
}

Now combine this to get the customization as you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文