【问题标题】:How control autosizing of object without IB?如何在没有 IB 的情况下控制对象的自动调整大小?
【发布时间】:2011-11-25 04:41:48
【问题描述】:
- (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 的情况下自然控制对象的自动调整大小?

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

【问题讨论】:

    标签: ios ios5 custom-controls rotation autosize


    【解决方案1】:

    您在视图上查找的属性是 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]
    

    【讨论】:

    【解决方案2】:

    你可以:

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

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

    您对来自willAnimateRotationToInterfaceOrientation:duration: 的视图的动画属性(frame、bounds、center、transform、alpha、backgroundColor、contentStretch)调用的任何更改也将在旋转期间进行动画处理。

    【讨论】:

    • 感谢您的回复。两种方法的区别因你而闻名。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多