0

我需要为我的应用程序支持横向和纵向方向,并且我冒险使用单个 UIView。

当我第一次模拟应用程序时,它会毫无问题地显示结果。但是,当我将方向更改为横向时,就会出现问题。

我第一次纵向运行应用程序时: 在此处输入图像描述

当我将方向更改为横向时: 在此处输入图像描述 注意左下角,靠近信息选项卡。

当我将方向改回纵向时: 在此处输入图像描述 它变得更糟。

我正在使用的代码,

- (void)updateLayoutForNewOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    CGFloat height = CGRectGetHeight([[UIScreen mainScreen] bounds]);
    CGFloat width = CGRectGetWidth([[UIScreen mainScreen] bounds]);

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        NSLog(@"Portrait");
        [self iPhoneUserInterfacePortrait:width height:height];
    }

    else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        NSLog(@"Landscape");
        [self iPhoneUserInterfaceLandscape:width height:height];
    }
}

- (void)iPhoneUserInterfacePortrait:(CGFloat)width height:(CGFloat)height
{
    UITextView *descriptionTextView = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, width - 100.0, height - 300.0)];

    [self makeBorder:descriptionTextView];
    [self setInformation:descriptionTextView];
    [self.view addSubview:descriptionTextView];
}

- (void)iPhoneUserInterfaceLandscape:(CGFloat)width height:(CGFloat)height
{
    UITextView *descriptionTextView = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, width + 230, height - 368.0)];

    [self makeBorder:descriptionTextView];
    [self setInformation:descriptionTextView];
    [self.view addSubview:descriptionTextView];
}

- (void)makeBorder:(UITextView *)descriptionTextView
{
    descriptionTextView.layer.borderWidth = 3.0;
    descriptionTextView.layer.cornerRadius = 15.0;
    descriptionTextView.layer.borderColor = [[UIColor grayColor] CGColor];

    [self.view addSubview:descriptionTextView];
}
4

1 回答 1

1

您正在添加多个视图,而您只需要一个视图。

[self.view addSubview:descriptionTextView];

要摆脱这一行,您可以将字段添加descriptionTextView到您的 ViewController 子类并更改该文本视图的框架,而无需从self.view. 此外,您应该尝试使用 AutoResizing 蒙版,看看是否可以在不实际手动更改帧的情况下获得所需的结果。你应该小心这些常量:在 3.5 和 4.0 英寸的模拟器设备上尝试你的应用程序。

于 2013-02-27T12:10:34.213 回答