ios11導航欄按鈕適配

前言

寫這篇文章,猶豫了很久。只能說提供了一個基本的分類,可以讓小伙伴更好,更快的調整ios11之后按鈕存在的問題,同時也適用ios11以下。【ios11按鈕適配解決方案,來自ios11導航欄適配】。

適配思路

這個UINavigationBarContentView平鋪在導航欄中作為iOS11的各個按鈕的父視圖,該視圖的所有的子視圖都會有一個layoutMargins被占用,也就是系統調整的占位,我們只要把這個置空就行了.那樣的話該視圖下的所有的子視圖的空間就會變成我們想要的那樣,當然為了保險起見,該視圖的父視圖也就是bar的layoutMargins也置空,這樣 整個bar就會跟一個普通視圖一樣了 左右的占位約束就不存在了。

適配文件說明

首先說一下這兩個文件。通過這兩個文件來解決ios11導航欄按鈕位置的偏移。直接拖進工程中即可。

這兩個文件用來調整占位

代碼

這部分是我自己封裝的一個超級簡單的類目。用來給導航欄添加按鈕時作為customeView。思路超級簡單,使用也很簡單。我自己寫了一個EDNavBarItem繼成UIView,在這個EDNavBarItem上添加按鈕,以便能夠更好的控制按鈕圖片和內容的位置。

- (instancetype)initWithPosition:(EDBarViewPosition)position itemImage:(UIImage *)image addActionHandler:(void(^)())handle {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, barItem_Width, barItem_Height);
        self.btn.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        if (position == EDBarViewPositionLeft) {
            [self.btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 15)];
            [self.btn setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
        } else {
            [self.btn setImageEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
            [self.btn setContentEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        }
        [self.btn setImage:image forState:UIControlStateNormal];
        [self.btn setImage:image forState:UIControlStateHighlighted];
        [self.btn addActionHandler:^(NSInteger tag) {
            if (handle) {
                handle();
            }
        }];
        [self addSubview:self.btn];
    }
    return self;
}

//EDBarViewPosition這是一個枚舉,用來判斷當前要創建的按鈕是位于左邊還是右邊按鈕。
//這里貼出代碼,按鈕的位置小伙伴可自行根據需要去調整位置。至于更多其它的情況,小伙幫可以自己去添加初始化創建的方法,我只考慮了這幾種情況,最多兩個圖片按鈕,一個文字圖片按鈕,文本按鈕。

UIViewController+EDBarButton是給UIViewController寫了一個類目,用于創建導航欄按鈕。貼出部分代碼。

/**
 添加左側導航欄帶圖片按鈕
 @param image 圖片
 @param handle 事件回調
 */
- (void)addLeftBarButtonWithImage:(UIImage *)image addActionHandler:(void(^)())handle;
/**
 添加右側帶圖片按鈕
 @param image 圖片
 @param handle 事件回調
 */
- (void)addRightBarButtonWithImage:(UIImage *)image addActionHandler:(void(^)())handle;
/**
 添加右側兩個帶圖片按鈕
 @param firstImage 第一張圖片
 @param firstHandle 事件一回調
 @param secondImage 第二張圖片
 @param selectImage 第二張選中圖片
 @param secondHandle 事件二回調
 */
- (void)addRightTwoBarButtonsWithFirstImage:(UIImage *)firstImage addFirstActionHandler:(void(^)())firstHandle secondImage:(UIImage *)secondImage secondSelectImage:(UIImage *)selectImage addSecondActionHandler:(void(^)(UIButton *barBtn))secondHandle;
/**
 添加左側兩個帶圖片按鈕
 @param firstImage 第一張圖片
 @param firstHandle 事件一回調
 @param secondImage 第二張圖片
 @param selectImage 第二張選中圖片
 @param secondHandle 事件二回調
 */
- (void)addLeftTwoBarButtonsWithFirstImage:(UIImage *)firstImage addFirstActionHandler:(void(^)())firstHandle secondImage:(UIImage *)secondImage secondSelectImage:(UIImage *)selectImage addSecondActionHandler:(void(^)(UIButton *barBtn))secondHandle;
/**
 設置右側文字按鈕
 @param title 標題
 @param handle 事件回調
 */
- (void)addRightBarButtonWithTitle:(NSString *)title addActionHandler:(void(^)())handle;
/**
 設置左側文字按鈕
 @param title 標題
 @param handle 事件回調
 */
- (void)addLeftBarButtonWithTitle:(NSString *)title addActionHandler:(void(^)())handle;
/**
 添加左側導航欄帶文字圖片按鈕
 @param image 文字圖片
 @param handle 事件回調
 */
- (void)addLeftBarButtonTextWithImage:(UIImage *)image addActionHandler:(void(^)())handle;
/**
 添加右側導航欄帶文字圖片按鈕
 @param image 文字圖片
 @param handle 事件
 */
- (void)addRightBarButtonTextWithImage:(UIImage *)image addActionHandler:(void(^)())handle;
- (void)addLeftBarButtonWithImage:(UIImage *)image addActionHandler:(void(^)())handle
{
    EDNavBarItem *barItem = [[EDNavBarItem alloc] initWithPosition:EDBarViewPositionLeft itemImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] addActionHandler:^{
        if (handle) {
            handle();
        }
    }];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:barItem];
    self.navigationItem.leftBarButtonItem = leftItem;

}
- (void)addRightBarButtonWithImage:(UIImage *)image addActionHandler:(void(^)())handle
{
    EDNavBarItem *barItem = [[EDNavBarItem alloc] initWithPosition:EDBarViewPositionRight itemImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] addActionHandler:^{
        if (handle) {
            handle();
        }
    }];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:barItem];
        self.navigationItem.rightBarButtonItem = rightItem;
}

具體使用

- (void)addRightItem {
    @weakify(self)
    [self addRightTwoBarButtonsWithFirstImage:[UIImage ed_imageNamedFromBundle:kFindingBundleName iconName:@"ed_finding_share"] addFirstActionHandler:^{
        @strongify(self)
        [self shareAction];
    } secondImage:[UIImage ed_imageNamedFromBundle:kFindingBundleName iconName:@"ed_finding_collect_btn_normal"] secondSelectImage:[UIImage ed_imageNamedFromBundle:kFindingBundleName iconName:@"ed_finding_collect_btn_select"] addSecondActionHandler:^(UIButton *barBtn) {
        @strongify(self)
        if (self.collectBtn == nil) {
            self.collectBtn = barBtn;
            return ;
        }
        [self collectAction:barBtn];
    }];
}

結尾

比較實用的小工具類目,很簡單,也沒什么復雜的邏輯。如果有什么問題,歡迎跟我交流,很愿意去修正問題,共同進步。文件下載地址點擊下載哦~提取密碼:856y。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容