日期选择器选择的叠加层在屏幕旋转时无法调整

问题描述 投票:0回答:1

我有一个视图,其中两个日期选择器设置为分别覆盖整个视图的高度和一半的宽度,以使它们填充整个视图。

然后,我向每个选择器添加了一个叠加层,以使选择更加可见,如下所示:

-(void)drawOverlays {
    if (_overlay1 != nil) {
        [_overlay1 removeFromSuperview];
    }
    if (_overlay2 != nil) {
        [_overlay2 removeFromSuperview];
    }
    _overlay1 = [[UIView alloc] initWithFrame:CGRectMake(_startPicker.bounds.origin.x, (_startPicker.frame.size.height/2)-19, _startPicker.bounds.size.width, 38)];
    _overlay1.backgroundColor = [UIColor redColor];
    _overlay1.alpha = 0.5f;
    [_startPicker addSubview:_overlay1];
    _overlay2 = [[UIView alloc] initWithFrame:CGRectMake(_endPicker.bounds.origin.x, (_endPicker.frame.size.height/2)-19, _endPicker.bounds.size.width, 38)];
    _overlay2.backgroundColor = [UIColor redColor];
    _overlay2.alpha = 0.5f;
    [_endPicker addSubview:_overlay2];
}

我从-viewDidLayoutSubviews方法和-viewWillTransitionToSize:withTransitionCoordinator方法调用此方法,并且第一次出现该视图时,一切都很好。

然后旋转我的iPad,并且叠加层显示为倒置,这意味着在landscape中,叠加层是我想要的portrait大小,反之亦然。

我的代码怎么了?

ios autolayout uiinterfaceorientation
1个回答
1
投票

使用约束并让自动布局处理调整大小会好得多:

-(void)drawOverlays {

    if (_overlay1 != nil) {
        [_overlay1 removeFromSuperview];
    }
    if (_overlay2 != nil) {
        [_overlay2 removeFromSuperview];
    }

    //_overlay1 = [[UIView alloc] initWithFrame:CGRectMake(_startPicker.bounds.origin.x, (_startPicker.frame.size.height/2)-19, _startPicker.bounds.size.width, 38)];

    // instantiate overlay1
    _overlay1 = [UIView new];
    _overlay1.backgroundColor = [UIColor redColor];
    _overlay1.alpha = 0.5f;

    // add as subview of startPicker
    [_startPicker addSubview:_overlay1];

    //_overlay2 = [[UIView alloc] initWithFrame:CGRectMake(_endPicker.bounds.origin.x, (_endPicker.frame.size.height/2)-19, _endPicker.bounds.size.width, 38)];

    // instantiate overlay2
    _overlay2 = [UIView new];
    _overlay2.backgroundColor = [UIColor redColor];
    _overlay2.alpha = 0.5f;

    // add as subview of endPicker
    [_endPicker addSubview:_overlay2];

    // we want to use auto-layout / constraints
    _overlay1.translatesAutoresizingMaskIntoConstraints = NO;
    _overlay2.translatesAutoresizingMaskIntoConstraints = NO;

    [NSLayoutConstraint activateConstraints:@[

        // constrain overlay1 to startPicker
        //  centerY
        //  leading / trailing = 0
        //  height = 38
        [_overlay1.centerYAnchor constraintEqualToAnchor:_startPicker.centerYAnchor],
        [_overlay1.leadingAnchor constraintEqualToAnchor:_startPicker.leadingAnchor constant:0.0],
        [_overlay1.trailingAnchor constraintEqualToAnchor:_startPicker.trailingAnchor constant:0.0],
        [_overlay1.heightAnchor constraintEqualToConstant:38.0],

        // constrain overlay2 to startPicker
        //  centerY
        //  leading / trailing = 0
        //  height = 38
        [_overlay2.centerYAnchor constraintEqualToAnchor:_endPicker.centerYAnchor],
        [_overlay2.leadingAnchor constraintEqualToAnchor:_endPicker.leadingAnchor constant:0.0],
        [_overlay2.trailingAnchor constraintEqualToAnchor:_endPicker.trailingAnchor constant:0.0],
        [_overlay2.heightAnchor constraintEqualToConstant:38.0],

        ]

     ];

}

而且,只需要从viewDidLoad调用它(或在其他任何合适的地方调用它)。不需要-实际上,应该[[not从viewDidLayoutSubviewsviewWillTransitionToSize中调用。

作为旁注–如果使用remove和re-add来显示和隐藏它们,则将它们添加一次,然后将.hidden属性设置为YES,也会得到更好的优化。或NO
© www.soinside.com 2019 - 2024. All rights reserved.