1、bug修改
UIView *bottomBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, bottomBarHeight)];
bottomBarView.backgroundColor = [UIColor whiteColor];
bottomBarView.bottom = self.view.bottom;
bottomBarView.tag = kDIYBottomBarViewTag;
[self.view addSubview:bottomBarView];
之前是根据bottom确定的bottomBarView的竖直位置。
而:self.view.bottom = self.view.frame.origin.y + self.view.frame.size.height;
那相当于:bottomBarView.bottom = self.view.frame.origin.y + self.view.frame.size.height;
相当于:bottomBarView.frame = CGRectMack(0, self.view.frame.origin.y + self.view.frame.size.height-bottomBarHeight,self.view.width, bottomBarHeight);
问题就在于,self.view是bottomBarView的父视图,[self.view addSubview:bottomBarView]的话,正常bottomBarView的frame应该为(0,self.view.heught-bottomBarView.height,self.view.width,bottomBarView.height)
bottomBarHeight == bottomBarView.height;
这样self.view.frame.origin.y是多出来的,当开启个人热点或来电话是,statusBar的高度由之前的20变成40,self.view.frame.origin.y由之前的0变成20,这样bottomBarView就整体下移了20. self.view.height减少了20;
这是改过的代码:
UIView *bottomBarView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height - bottomBarHeight, self.view.width, bottomBarHeight)];
bottomBarView.backgroundColor = [UIColor whiteColor];
//bottomBarView.bottom = self.view.bottom;
bottomBarView.tag = kDIYBottomBarViewTag;
[self.view addSubview:bottomBarView];
总结:当两个视图存在父子关系是,最好不要用bottom属性传值,因为他们的相对坐标原点不同,子视图的(0,0)位置是父视图的(origin.x,origin.y)位置,bottom的计算就存在误差。(frame.origin.y的改变)
2、造成视图下移的其它原因:
SCREEN_HEIGHT的使用,当个人热点形成,statusBar变为40,self.view.height和self.view.bounds.height都减少20,而此时再用SCREEN_HEIGHT为基准计算视图高度或位置时,因为SCREEN_HEIGHT的大小并没有改变,所以造成适配偏差。
备注:
1、
-(CGFloat)bottom {
return self.frame.origin.y + self.frame.size.height;
}
2、
#define SCREEN_HEIGHT (IsPortrait ? MAX(([UIScreen mainScreen].bounds.size.width), ([UIScreen mainScreen].bounds.size.height)) : MIN(([UIScreen mainScreen].bounds.size.width), ([UIScreen mainScreen].bounds.size.height)))