iOS自定义键盘加载问题

时间:2015-04-06 16:21:05

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

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

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

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

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

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

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

任何建议都会很棒!

1 个答案:

答案 0 :(得分:0)

 - (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];
        }
    }

请针对您的问题试用此代码。首先使用此代码做一些R&amp; D并告诉我。

相关问题