如何在没有 IB 的情况下控制对象的自动调整大小?

发布于 2024-12-18 02:44:39 字数 876 浏览 1 评论 0原文

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{

    return YES;

}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

    [self positionViews];  

}


-(void)positionViews {

    UIInterfaceOrientation destOrientation = self.interfaceOrientation;
    if (destOrientation == UIInterfaceOrientationPortrait ||
        destOrientation == UIInterfaceOrientationPortraitUpsideDown) {

        [bLabel setFrame:CGRectMake(255, 10, 270, 60)];

    } else { 


        [bLabel setFrame:CGRectMake(377, 10, 270, 60)];

    }   
}

bLabel 是自定义对象。

当 iPad(设备)旋转时,bLable 也会旋转。 但出现时间滞后。 因此旋转运动不平滑。

我的代码有什么错误? 如何自然地控制没有 IB 的对象的自动调整大小?

请告诉我你的建议。谢谢!!!

在此处输入图像描述

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{

    return YES;

}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

    [self positionViews];  

}


-(void)positionViews {

    UIInterfaceOrientation destOrientation = self.interfaceOrientation;
    if (destOrientation == UIInterfaceOrientationPortrait ||
        destOrientation == UIInterfaceOrientationPortraitUpsideDown) {

        [bLabel setFrame:CGRectMake(255, 10, 270, 60)];

    } else { 


        [bLabel setFrame:CGRectMake(377, 10, 270, 60)];

    }   
}

bLabel is Cutomed Object.

When IPad(device) rotates, bLable is rotated.
But appear time lag.
So rotation motion is not smooth.

What is my code's faults?
How control autosizing of object without IB naturally?

Please tell me your advice. Thanks!!!

enter image description here

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

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

发布评论

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

评论(2

童话 2024-12-25 02:44:39

您在视图上查找的属性是 autoresizingMask。就像在 IB 中一样设置它。以下是可用的值:

UIViewAutoresizing
Specifies how a view is automatically resized.

enum {
   UIViewAutoresizingNone                 = 0,
   UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
   UIViewAutoresizingFlexibleWidth        = 1 << 1,
   UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
   UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
   UIViewAutoresizingFlexibleHeight       = 1 << 4,
   UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};

例如,如果您想要具有固定边距的灵活宽度和高度,您可以这样做:

[myView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoResizingFlexibleWidth;][1]

The property you are looking for on the view is autoresizingMask.Set this just as you would in IB. Here are the values available:

UIViewAutoresizing
Specifies how a view is automatically resized.

enum {
   UIViewAutoresizingNone                 = 0,
   UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
   UIViewAutoresizingFlexibleWidth        = 1 << 1,
   UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
   UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
   UIViewAutoresizingFlexibleHeight       = 1 << 4,
   UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};

For example, if you want a flexible width and height with fixed margins you would do:

[myView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoResizingFlexibleWidth;][1]
眉黛浅 2024-12-25 02:44:39

您可以:

  1. 设置 UIView 的 autoresizingMask(如果可以)
    自动为您调整大小。

    bLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    
  2. 如果两个设备方向之间的布局差异太大,您还可以在旋转动画期间调用的 UIViewController 方法 willAnimateRotationToInterfaceOrientation:duration: 中更改视图,而不是使用 didRotateFromInterfaceOrientation: 在动画事务之后调用。

您从 willAnimateRotationToInterfaceOrientation:duration: 对视图的可动画属性(frame、bounds、center、transform、alpha、backgroundColor、contentStretch)调用的任何更改也将在旋转。

You can either :

  1. set your UIView's autoresizingMask, if it can be
    automatically resized for you.

    bLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    
  2. If the layout between 2 device orientation is too different, you can also change your views in the UIViewController's method willAnimateRotationToInterfaceOrientation:duration: which is called during rotation animation, instead of using didRotateFromInterfaceOrientation: which is called AFTER the animation transaction.

Any changes you call on your views' animatable properties (frame, bounds, center, transform, alpha, backgroundColor, contentStretch) from willAnimateRotationToInterfaceOrientation:duration: will also be animated during the rotation.

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