這個demo是爲解決IQKeyboardManager和Masonry同時使用時,導航欄上移和make.right失效的問題

原文連接在個人我的博客主頁git

(一)、引言:

IQKeyboardManagerMasonry 同時使用時,導航欄上移和make.right失效等問題多多。github

其實咱們完美的效果應該是這樣的:*(NO Pictures say *8 !O(∩_∩)O~)*佈局

(二)、問題介紹:

咱們使用 IQKeyboardManager 能夠很好的處理鍵盤彈起輸入框上移事件。可是當你的 backView 【底視圖】不是 tableView 或者scrollView 時。你的導航欄會隨着一塊兒往上跑了。優化

就像這樣:code

若是是上圖那種效果。你的產品經理會放過你這個逗比嗎?orm

不!!!,毫不會。必定會說:「重作。導航欄不能往上跑。」blog

好吧。不往上跑。因而你在網上會找到 以下方法解決了這個問題:事件

-(void)loadView {
        
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [scrollView setBackgroundColor:[UIColor grayColor]];
    self.view = scrollView;
}

可是雖然不往上跑了。尼瑪又出現了其餘問題:get

像這樣:
博客

哎呀,我擦:

怎麼個人控件佈局都亂了。

【本屌也是在這個地方卡蠻久,最後本身摸索出了本文章的解決辦法。】

在通過屢次嘗試以後你會發現。真正的問題所在是 IQKeyboardManagerMasonry 同時使用時,控件放在 scrollView上面。masonrymake.right 約束就會失效。
可是 make.width 等等其餘約束仍是正常的。

你能夠不使用 make.right 約束,用 make.widthmake.left代替約束。可是我以爲仍是用 make.rightmake.left 約束組合要好些。不要總是寫個 make.width的固定寬度。

(三)、需求目的:

咱們想要的效果很簡單。就如文章開篇的圖一那樣。。控件佈局正常,鍵盤彈起時相應的輸入框要上擡。可是啊,這個導航欄是堅定不能也上擡的。同時支持 make.right 約束。

(四)、解決方法:

  • 1.重寫 loadView 方法 。把 self.view 替換成 scrollView

  • 2.背景容器視圖(back)必須設置。並且對 back 約束時 要附帶 make.width.mas_equalTo(self.view);【不寫致使 textField 佈局的 make.right 失效】

  • 3.子控件要直接放在self.view 上。不能放在背景容器視圖(back)上面。【放在 back上時會沒法點擊,沒法成爲第一響應】

(方法中有點腦殘的地方就是設置了 backView 底視圖可是沒有用它。還沒想到好的優化方法,先就實現需求而言想出的這個搓比方法。)
【附上本demo的垃圾代碼以下:】

//
//  ViewController.m
//  IQKeyboardManagerAndMasonryConflictDemo
//
//  Created by Mingo on 17/4/6.
//  Copyright © 2017年 Mingo. All rights reserved.
//
    
#import "ViewController.h"
#import <Masonry/Masonry.h>
    
    
@interface ViewController ()
    
@end
    
@implementation ViewController
    
#pragma mark - step 01
-(void)loadView { //不將 self.view 替換成 scrollView 會在點擊底部輸入框時 導航欄也一塊兒往上跑。
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [scrollView setBackgroundColor:[UIColor grayColor]];
    self.view = scrollView;
}
    
/**  
 1.重寫 loadView 方法 。把 self.view 替換成 scrollView。
 
 2.背景容器視圖(back)必須設置。並且對 back 約束時 要附帶 make.width.mas_equalTo(self.view);
 【不寫致使 textField 佈局的 make.right 失效】
 
 3.子控件要直接放在self.view 上。不能放在背景容器視圖(back)上面。
 【放在 back上時會沒法點擊,沒法成爲第一響應】
 
 */
    
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我是導航欄";
    
#pragma mark - step 02
    UIView  *back = [[UIView alloc] init];
    [self.view addSubview:back];
    [back mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.edges.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view); 
        //此處必填 - 【關鍵點】 。不寫致使 textField 佈局的 make.right 失效。
        //(可是佈局textField 時使用 make.width不受這句話限制。)
    }];
    
    
    for (int i = 0 ; i < 30 ; i++) {
       
        UITextField *textField = [[UITextField alloc] init];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.placeholder =  [NSString stringWithFormat:@"%d請輸入文字",i];
        
#pragma mark - step 03
        [self.view addSubview:textField];
 //      [back addSubview:textField];   
 //      textField 放在 back上時會沒法點擊,沒法成爲第一響應。
        
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.with.offset(20);
            make.right.with.offset(-20);
            make.height.mas_equalTo(30);
            make.top.mas_equalTo(i *40+5);
        }];
    }
}
    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
    
    
@end

完整的 demo 已經上傳 github 中:
https://github.com/yfming93/IQKeyboarManagerAndMasonryConflictDemo

相關文章
相關標籤/搜索