2

嗨,我已经为 iPhone4 开发了应用程序,现在我想将应用程序转换为 iPhone5,我有一个名为 settingsView 的 UIView,它是通过单击 UIButton 弹出的。我想知道如何为 iPhone5 调整 settingsView 的高度。

 -(IBAction)settingsButtonChanged:(UIButton *)sender
{
    UIImageView *settingsImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"settingsViewImage.png"]];
    settingsImage.frame = CGRectMake(25.0, 40.0, 280.0, 370.0);


    settingsView.frame = CGRectMake(0.0, 20.0, 280.0, 370.0);
    settingsView.backgroundColor = [UIColor clearColor];
    [settingsView addSubview:settingsImage];

}
4

5 回答 5

4

如果您使用新的自动布局系统,您可以设置约束来自动调整视图的布局以利用屏幕大小。例如,如果您在视图顶部附近有几个元素,中间有一个表格,底部附近有一些按钮,您可以添加以下约束:

  • 使顶部元素与视图顶部保持固定距离
  • 保持顶部元素之间的间距固定
  • 使底部按钮与视图底部保持固定距离
  • 调整表格的高度,使表格在较大的屏幕上增大,在较小的屏幕上缩小

您可以自己计算并进行调整,但自动布局系统的全部意义在于您不必费心——让视图为您完成。

于 2013-02-14T13:40:11.080 回答
3

也许,您想使用两个故事板,一个用于 iPhone 3.5",另一个用于 iPhone 4"。通过将此代码放在您的应用程序委托上,您将能够使这些故事板工作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0){
            //move to your iphone5 storyboard
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iphoneFiveStoryboard" bundle:[NSBundle mainBundle]];
            UIViewController *vc =[storyboard instantiateInitialViewController];

            self.window.rootViewController = vc;
            [self.window makeKeyAndVisible];
        }
        else{
            //move to your iphone4s storyboard
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
            UIViewController *vc =[storyboard instantiateInitialViewController];

            self.window.rootViewController = vc;
            [self.window makeKeyAndVisible];
        }}
    // Override point for customization after application launch.
    return YES;
}

现在您只需要转到:文件 -> 新建... -> 文件 -> 用户界面 -> 故事板

并将情节提要命名为“iphoneFiveStoryboard”...

现在您可以用 4" 视图重新制作所有故事板!

希望你能理解,我试图解释我能做的最好的......

于 2013-02-14T14:22:37.377 回答
2

使用self.view.frame.size.height,您可能需要将其除以某个值以匹配我注意到的宽度比例 320:280

-(IBAction)settingsButtonChanged:(UIButton *)sender
{
    UIImageView *settingsImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"settingsViewImage.png"]];
    settingsImage.frame = CGRectMake(25.0, 40.0, 280.0, self.view.frame.size.height);
    settingsView.backgroundColor = [UIColor clearColor];
    [settingsView addSubview:settingsImage];

}
于 2013-02-14T13:29:36.157 回答
2

您必须检查在代码行之上iPhone 4iPhone 5之下运行的应用程序是否可以帮助您:-

CGRect screenBounds = [[UIScreen mainScreen] bounds];
        if (screenBounds.size.height == 568)
        {
            // code for 4-inch screen
            //for eg:-
            [self.view setFrame:CGRectMake(0, 0, 320, 568)];
        }
于 2013-02-14T13:32:00.383 回答
0

你可以试试这个-

-(IBAction)settingsButtonChanged:(UIButton *)sender
{
    UIImageView *settingsImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"settingsViewImage.png"]];
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    int height = 0;
    if (screenBounds.size.height == 568) {
        // Height for iPhone 5 screen
        height = 370+ 88; // it may be your desired height
    } else {
        // Height for iPhone 4 screen
       height = 370;
    }
    settingsImage.frame = CGRectMake(25.0, 40.0, 280.0, height);
    settingsView.frame = CGRectMake(0.0, 20.0, 280.0, height);
    settingsView.backgroundColor = [UIColor clearColor];
    [settingsView addSubview:settingsImage];

}
于 2013-08-25T22:13:06.307 回答