iOS11NavigationItem偏移,iOS11適配問題,iOS11導航欄返回偏移,iOS11BarButtonItem偏移,Xcode9遇見的問題
更新iOS 11之後,用xcode 9執行App,你會發現以下問題:
注意:此文章程式碼做了更新,對於item點選不靈敏問題,文章結尾寫出瞭解決辦法。
1、MJ重新整理異常
2、tableView的section之間間距變大,空白區域
3、導航欄返回按鈕偏移20畫素
下面我逐個講一下解決辦法:
1、MJ重新整理異常,上拉加載出現跳動重新整理問題:MJ已經更新,解決了此bug
2、tableView的section之間間距變大問題:解決辦法:初始化的時候增加以下程式碼
self.tableView.estimatedRowHeight =0;
self.tableView
self.tableView.estimatedSectionFooterHeight =0;
3、導航欄按鈕偏移20畫素問題:
解決辦法:
樓主寫的分類:UIViewController+BarButton
程式碼如下:
//左側一個圖片按鈕的情況
- (void)addLeftBarButtonWithImage:(UIImage *)image action:(SEL)action
{
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,44,44)];
view.
UIButton *firstButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
firstButton.frame = CGRectMake(0, 0, 44, 44);
[firstButton setImage:imageforState:UIControlStateNormal];
[firstButton addTarget:selfaction:actionforControlEvents:UIControlEventTouchUpInside];
firstButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
[firstButton setImageEdgeInsets:UIEdgeInsetsMake(0,5 *kScreenWidth /375.0,0,0)];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:firstButton];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
}
//右側一個圖片按鈕的情況
- (void)addRightBarButtonWithFirstImage:(UIImage *)firstImage action:(SEL)action
{
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,44,44)];
view.backgroundColor = [UIColorclearColor];
UIButton *firstButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
firstButton.frame = CGRectMake(0, 0, 44, 44);
[firstButton setImage:firstImageforState:UIControlStateNormal];
[firstButton addTarget:selfaction:actionforControlEvents:UIControlEventTouchUpInside];
firstButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
[firstButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,0,5 *kScreenWidth /375.0)];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:firstButton];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}
//右側為文字item的情況
- (void)addRightBarButtonItemWithTitle:(NSString *)itemTitle action:(SEL)action
{
UIButton *rightbBarButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,88,44)];
[rightbBarButton setTitle:itemTitleforState:(UIControlStateNormal)];
[rightbBarButton setTitleColor:kDarkOneColorforState:(UIControlStateNormal)];
rightbBarButton.titleLabel.font = [UIFontsystemFontOfSize:14];
[rightbBarButton addTarget:selfaction:actionforControlEvents:(UIControlEventTouchUpInside)];
rightbBarButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
[rightbBarButton setTitleEdgeInsets:UIEdgeInsetsMake(0,0,0,5 *kScreenWidth/375.0)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:rightbBarButton];
}
//左側為文字item的情況
- (void)addLeftBarButtonItemWithTitle:(NSString *)itemTitle action:(SEL)action
{
UIButton *leftbBarButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,44,44)];
[leftbBarButton setTitle:itemTitleforState:(UIControlStateNormal)];
[leftbBarButton setTitleColor:kDarkOneColorforState:(UIControlStateNormal)];
leftbBarButton.titleLabel.font = [UIFontsystemFontOfSize:16];
[leftbBarButton addTarget:selfaction:actionforControlEvents:(UIControlEventTouchUpInside)];
leftbBarButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
[leftbBarButton setTitleEdgeInsets:UIEdgeInsetsMake(0,5*kScreenWidth/375.0,0,0)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:leftbBarButton];
}
//右側兩個圖片item的情況- (void)addRightTwoBarButtonsWithFirstImage:(UIImage *)firstImage firstAction:(SEL)firstAction secondImage:(UIImage *)secondImage secondAction:(SEL)secondAction
{
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,80,44)];
view.backgroundColor = [UIColorclearColor];
UIButton *firstButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
firstButton.frame = CGRectMake(40, 0, 40, 44);
[firstButton setImage:firstImageforState:UIControlStateNormal];
[firstButton addTarget:selfaction:firstActionforControlEvents:UIControlEventTouchUpInside];
firstButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
[firstButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,0,5 *kScreenWidth/375.0)];
[view addSubview:firstButton];
UIButton *secondButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
secondButton.frame = CGRectMake(0, 0, 40, 44);
[secondButton setImage:secondImageforState:UIControlStateNormal];
[secondButton addTarget:selfaction:secondActionforControlEvents:UIControlEventTouchUpInside];
secondButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
[secondButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,0,0 *kScreenWidth/375.0)];
[view addSubview:secondButton];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:view];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}
//右側三個圖片item的情況- (void)addRightThreeBarButtonsWithFirstImage:(UIImage *)firstImage firstAction:(SEL)firstAction secondImage:(UIImage *)secondImage secondAction:(SEL)secondAction thirdImage:(UIImage *)thirdImage thirdAction:(SEL)thirdAction
{
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,120,44)];
view.backgroundColor = [UIColorclearColor];
UIButton *firstButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
firstButton.frame = CGRectMake(80, 0, 40, 44);
[firstButton setImage:firstImageforState:UIControlStateNormal];
[firstButton addTarget:selfaction:firstActionforControlEvents:UIControlEventTouchUpInside];
firstButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
[firstButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,0,5 *kScreenWidth/375.0)];
[view addSubview:firstButton];
UIButton *secondButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
secondButton.frame = CGRectMake(44, 0, 40, 44);
[secondButton setImage:secondImageforState:UIControlStateNormal];
[secondButton addTarget:selfaction:secondActionforControlEvents:UIControlEventTouchUpInside];
secondButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
[secondButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,0,8 *kScreenWidth/375.0)];
[view addSubview:secondButton];
UIButton *thirdButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
thirdButton.frame = CGRectMake(0, 0, 40, 44);
[thirdButton setImage:thirdImageforState:UIControlStateNormal];
[thirdButton addTarget:selfaction:thirdActionforControlEvents:UIControlEventTouchUpInside];
[view addSubview:thirdButton];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:view];
}
在你的控制器裡邊,直接[self addleft...]就可以呼叫,如果覺得座標有問題,可以自行處理變更
如果你的需求是三個四個item按鈕,可以仿照上邊兩個item按鈕的方法,自行處理。
有問題請留言,我看到後會及時處理。
注意:以上我寫的方法,已經不會偏移了。
但是會出現點選不到item情況。借鑑了UINavigation-SXFixSpace的幾個分類,解決了我的問題。謝謝此位大哥的分享。