【问题标题】:iOS custom keyboard loading issueiOS自定义键盘加载问题
【发布时间】:2015-06-11 01:55:24
【问题描述】:

我正在使用 Objective-C 在 iOS 上创建自定义键盘。为了增加键盘的高度以使其与纵向和横向的默认键盘高度(带有建议栏)相同,我以编程方式添加了高度约束{我在添加新约束之前删除了先前添加的约束}。

我正在更新类的viewWillLayoutSubviews 方法中的高度,以便在方向改变时,达到所需的键盘高度并且它工作得很好。

但我的问题是,在方向更改时,视图不会像在 iOS 上的默认键盘中那样完美地自动布局,而是视图会更改,然后更新高度,然后扩展到屏幕右侧以覆盖整个区域. (基本上在键盘完全加载之前发生了混蛋)

此外,在初始化键盘时,视图会以默认高度加载,然后会更新自定义高度。发布后,它会应用自动布局约束并更新 UI,从而提供键盘屏幕的完整布局。整个过程看起来有点生涩。

我正在使用带有自动布局的 xib。 如果我不更新键盘的自定义高度,上面提到的问题已经解决,但键盘的高度仍然是苹果允许的默认高度。

还有一点,XIB 中有很多视图,因此有很多限制。如果我删除约束,键盘加载速度非常快,并且还可以实现自定义高度。但是,一旦我旋转设备,视图就不会布局,并且我在屏幕上被半键盘卡住了。

任何建议都会很棒!

【问题讨论】:

    标签: ios objective-c xcode6 nslayoutconstraint custom-keyboard


    【解决方案1】:
     - (id)initWithCoder:(NSCoder *)aDecoder {
            self = [super initWithCoder:aDecoder];
            if (self) {
                // Perform custom initialization work here
    
                if (IS_IPAD) {
                    self.portraitHeight = 300;
                    self.landscapeHeight = 300;
                }else{
                    self.portraitHeight = 257;
                    self.landscapeHeight = 203;
                }
            }
            return self;
        }
    
        - (void)updateViewConstraints {
            [super updateViewConstraints];
    
            // Add custom view sizing constraints here
            if (_viewWillAppearExecuted)
                [self adjustHeight];
    
           }
        -(void)viewWillAppear:(BOOL)animated{
            [super viewWillAppear:YES];
    
            [self.view addConstraint:self.heightConstraint];
            _viewWillAppearExecuted = YES;
        }
        #pragma mark - Setters/Getters
    
        - (NSLayoutConstraint *)heightConstraint
        {
            if (!_heightConstraint) {
                _heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:self.portraitHeight];
    
                _heightConstraint.priority = UILayoutPriorityRequired - 1;
            }
    
            return _heightConstraint;
        }
    
    
        #pragma mark - Methods
    
        - (void)adjustHeight
        {
            if (self.view.frame.size.width == 0 || self.view.frame.size.height == 0)
                return;
    
            [self.view removeConstraint:self.heightConstraint];
        //    [self.imageView removeConstraint:self.heightConstraint];
            CGSize screenSize = [[UIScreen mainScreen] bounds].size;
            CGFloat screenH = screenSize.height;
            CGFloat screenW = screenSize.width;
            BOOL isLandscape =  !(self.view.frame.size.width ==
                                  (screenW*(screenW<screenH))+(screenH*(screenW>screenH)));
    
            self.isLandscape = isLandscape;
            if (isLandscape) {
                self.heightConstraint.constant = self.landscapeHeight;
        //        [self.imageView addConstraint:self.heightConstraint];
                [self.view addConstraint:self.heightConstraint];
            } else {
                self.heightConstraint.constant = self.portraitHeight;
        //        [self.imageView addConstraint:self.heightConstraint];
                [self.view addConstraint:self.heightConstraint];
            }
        }
    

    试试这个代码来解决你的问题。首先用这段代码做一些研发,让我知道。

    【讨论】:

    • 还添加这些属性(非原子) CGFloat PortraitHeight;属性(非原子)CGFloat LandscapeHeight;属性(非原子) BOOL isLandscape;属性(非原子) NSLayoutConstraint *heightConstraint; @property (nonatomic) BOOL viewWillAppearExecuted;
    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 2014-11-29
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多