【问题标题】:Date pickers selection's overlay not adjusting when screen rotates屏幕旋转时日期选择器选择的覆盖不调整
【发布时间】:2020-01-13 12:01:18
【问题描述】:

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

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

-(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】:

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

    -(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(或您认为合适的其他任何地方)调用。没有必要——事实上,它应该被——从viewDidLayoutSubviewsviewWillTransitionToSize 调用。

    附带说明——如果您使用 remove 和 re-add 来显示和隐藏它们,如果您将它们添加一次,然后将 .hidden 属性设置为 @987654326,您还将获得更好的优化@ 或 NO

    【讨论】:

      猜你喜欢
      • 2014-01-05
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多