1

UIView用两个文本标签和两个按钮进行了自定义。它在创建时功能齐全,但现在我试图在屏幕旋转时移动它(即从纵向到横向)。

尽管 UI 元素似乎被正确移动,但实际UIView的框架最终会做一些奇怪的事情,比如变成一个矩形,当它最初是一个正方形并且被设置为一个正方形但具有不同的 x、y 位置时。

我看到这一点的方法是将我UIView的背景颜色设置为黑色并观察我的背景图像被绘制在我希望绘制视图的位置,但UIView背景变成了不同的形状并且被绘制在错误的位置。

这是我的代码:

- (void) moveControls
{
    CGRect frame;

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    switch (orientation)
    {
        case UIDeviceOrientationPortrait:
        case UIDeviceOrientationPortraitUpsideDown:
            frame = CGRectMake(0, 120, 320, 320);
            break;
        case UIDeviceOrientationLandscapeLeft:
        case UIDeviceOrientationLandscapeRight:
            frame = CGRectMake(120, 0, 320, 320);
            break;
        case UIDeviceOrientationFaceDown:
        case UIDeviceOrientationFaceUp:
        case UIDeviceOrientationUnknown:
           return;
    }

    [self setFrame:frame];

    backgroundImage.frame = CGRectMake(0,0, frame.size.width, frame.size.height);
    titleLabel.frame = CGRectMake(0,50,frame.size.width,frame.size.height/2);
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    textLabel.frame = CGRectMake(0,frame.size.height/3,frame.size.width,frame.size.height/3);
    doneButton.frame = CGRectMake(frame.size.width - 154, frame.size.height - 90, 100, 34);
    tellButton.frame =CGRectMake(54, frame.size.height - 90, 100, 34);
}
4

2 回答 2

4

并没有真正回答所有问题,但解决了问题: ios: UIView's width and height after changed orientation is inaccurate?

view.center = view.superview.center;
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |  UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
于 2013-04-19T19:24:30.983 回答
0

利用:

- (void) moveControls
{
    CGRect frame;

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (UIDeviceOrientationIsLandscape(orientation))
        frame = CGRectMake(120, 0, 320, 320);
    else
        frame = CGRectMake(0, 120, 320, 320);



    [self setFrame:frame];

    backgroundImage.frame = CGRectMake(0,0, frame.size.width, frame.size.height);
    titleLabel.frame = CGRectMake(0,50,frame.size.width,frame.size.height/2);
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    textLabel.frame = CGRectMake(0,frame.size.height/3,frame.size.width,frame.size.height/3);
    doneButton.frame = CGRectMake(frame.size.width - 154, frame.size.height - 90, 100, 34);
    tellButton.frame =CGRectMake(54, frame.size.height - 90, 100, 34);
}
于 2013-04-19T17:01:36.930 回答