-
不要在 - (void)viewDidLoad; 方法里獲取Xib里面的frame值:
舉例:在一個XIb中含約束的View中, 代碼添加一個自定義動態(tài)View;
(1) XIb約束如下:
(2) 添加子View代碼
CGRect backgroudViewFrame = CGRectMake(0, 0, CGRectGetWidth(self.backgroudView.bounds), CGRectGetHeight(self.backgroudView.bounds));
UIView *frontView = [[UIView alloc] initWithFrame:backgroudViewFrame];
frontView.backgroundColor = [UIColor yellowColor];
[self.backgroudView addSubview:frontView];
NSLog(@"%s,frame = %@",__func__,self.backgroudView);
(3)在 -(void)viewDidLoad 和 - (void)viewDidAppear:(BOOL)animated中的輸出分別是
-[ViewController viewDidLoad],frame = <UIView: 0x78792640; frame = (36 70; 248 448); autoresize = RM+BM; layer = <CALayer: 0x78792810>>
-[ViewController viewDidAppear:],frame = <UIView: 0x78792640; frame = (36 70; 248 360); autoresize = RM+BM; layer = <CALayer: 0x78792810>>
總結:可以看到在viewDidLoad 和 viewDidAppera中獲取的frame不一致。 在動態(tài)獲取frame中,應該在viewDidApear中獲取.