@property(nonatomic) BOOL autoresizesSubviews;
// default is YES. if set, subviews are adjusted according to their autoresizingMask if self.bounds changes
使用loadNibNamed創(chuàng)建了一個view戳葵,設(shè)置了它的frame式散,但最終顯示出來的大小竟然不對拯欧。就一個地方設(shè)置了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]);
//此處加入斷點(diǎn),以便查看棧的內(nèi)容
}
結(jié)果顯示琼梆,
_vHead.frame = CGRectMake(0, 0, w, y);
改變了self.vSearch的frame性誉。而vHead是vSearch的superView窿吩。這就比較明了了,我也沒加約束错览,so纫雁,autoresizes有問題。解決這個問題就很簡單了倾哺,設(shè)置
self.vSearch.autoresizingMask = UIViewAutoresizingNone;
或者
_vHead.autoresizesSubviews = NO;
KVO+斷點(diǎn)+棧信息解決這類問題不錯轧邪。