美文网首页
autoresizesSubviews导致frame不对

autoresizesSubviews导致frame不对

作者: 观星 | 来源:发表于2016-06-16 18:36 被阅读402次

    @property(nonatomic) BOOL autoresizesSubviews;
    // default is YES. if set, subviews are adjusted according to their autoresizingMask if self.bounds changes

    使用loadNibNamed创建了一个view,设置了它的frame,但最终显示出来的大小竟然不对。就一个地方设置了frame,打印前后的frame,都是正确的,没办法,使用KVO看看谁对它动了手脚。

    [self.vSearch addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
     NSLog(@"%@",[change objectForKey:NSKeyValueChangeOldKey]);
        NSLog(@"%@",[change objectForKey:NSKeyValueChangeNewKey]);
        //此处加入断点,以便查看栈的内容
    }
    

    结果显示,

    _vHead.frame = CGRectMake(0, 0, w, y);
    

    改变了self.vSearch的frame。而vHead是vSearch的superView。这就比较明了了,我也没加约束,so,autoresizes有问题。解决这个问题就很简单了,设置

    self.vSearch.autoresizingMask = UIViewAutoresizingNone;
    

    或者

    _vHead.autoresizesSubviews = NO;
    

    KVO+断点+栈信息解决这类问题不错。

    相关文章

      网友评论

          本文标题:autoresizesSubviews导致frame不对

          本文链接:https://www.haomeiwen.com/subject/hsgmdttx.html