2018-11-11

2018-11-11  本文已影响0人  洛河水手

iOS 11 下 iPhone X 键盘上 输入语言切换按钮点击无反应问题。如下图所示:

iPhone X键盘语言切换

找了好久最终参考stackoverflow上的解决方法定位了原因,附上链接

ios11 - iOS 11 blank keyboard and internal layout conflict in UIInputSwitcherTableCellSegmentView - Stack Overflow

在工程中使用了一个UIButton的分类,导致了这个问题,分类实现如下:

#import <UIKit/UIKit.h>

@interfaceUIButton (Drag)

@property(nonatomic,assign,getter= isDragEnable)  BOOL dragEnable;

@property(nonatomic,assign,getter= isAdsorbEnable)BOOLadsorbEnable;

@end

#import "UIButton+Drag.h"

#import <objc/runtime.h>

#define PADDING    5

#define BOTTOMPADDING  88

#define BUTTONWIDTH    70

static void *DragEnableKey = &DragEnableKey;

static void *AdsorbEnableKey = &AdsorbEnableKey;

@implementationUIButton (Drag)

-(void)setDragEnable:(BOOL)dragEnable{

    objc_setAssociatedObject(self, DragEnableKey,@(dragEnable), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

-(BOOL)isDragEnable{

    return [objc_getAssociatedObject(self, DragEnableKey) boolValue];

}

-(void)setAdsorbEnable:(BOOL)adsorbEnable{

    objc_setAssociatedObject(self, AdsorbEnableKey,@(adsorbEnable), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

-(BOOL)isAdsorbEnable{

    return [objc_getAssociatedObject(self, AdsorbEnableKey) boolValue];

}

CGPointbeginPoint;

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{

    self.highlighted = YES;

    if (![objc_getAssociatedObject(self, DragEnableKey) boolValue]) {

        return;

    }

   UITouch*touch = [touchesanyObject];

   beginPoint = [touch locationInView:self];

}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{

    self.highlighted = NO;

    if (![objc_getAssociatedObject(self, DragEnableKey) boolValue]) {

        return;

    }

   UITouch*touch = [touchesanyObject];

   CGPointnowPoint = [touchlocationInView:self];

   floatoffsetX = nowPoint.x-beginPoint.x;

    floatoffsetY = nowPoint.y-beginPoint.y;

   if(fabsf(offsetX) >0.3||fabsf(offsetY) >0.3) {//解决点击和拖动冲突的问题

        self.highlighted = NO;

    }else{

        self.highlighted = YES;

    }

    self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);

}

-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{

    if (self.tag == 55551 || self.tag == 55552 || self.tag == 55553 || self.tag == 55554) {

        if (self.highlighted) {

            [self sendActionsForControlEvents:UIControlEventTouchUpInside];

            self.highlighted = NO;

        }

    }else{

        [self sendActionsForControlEvents:UIControlEventTouchUpInside];

        self.highlighted = NO;

    }

    if (self.superview && [objc_getAssociatedObject(self,AdsorbEnableKey) boolValue] ) {

        floatmarginLeft =self.frame.origin.x;

        float marginRight = self.superview.frame.size.width - self.frame.origin.x - self.frame.size.width;

        floatmarginTop =self.frame.origin.y;

        float marginBottom = self.superview.frame.size.height - self.frame.origin.y - self.frame.size.height - BOTTOMPADDING;

        [UIView animateWithDuration:0.125 animations:^(void){

            if(marginTop

                self.frame = CGRectMake(marginLeft<marginRight?marginLeft<PADDING?PADDING:self.frame.origin.x:marginRight<PADDING?self.superview.frame.size.width -self.frame.size.width-PADDING:self.frame.origin.x,

                                        PADDING,

                                        self.frame.size.width,

                                        self.frame.size.height);

            }

            elseif(marginBottom

                self.frame = CGRectMake(marginLeft<marginRight?marginLeft<PADDING?PADDING:self.frame.origin.x:marginRight<PADDING?self.superview.frame.size.width -self.frame.size.width-PADDING:self.frame.origin.x,

                                        self.superview.frame.size.height - self.frame.size.height-BOTTOMPADDING,

                                        self.frame.size.width,

                                        self.frame.size.height);

            }

            else{

                self.frame = CGRectMake(marginLeft<marginRight?PADDING:self.superview.frame.size.width - self.frame.size.width-PADDING,

                                        self.frame.origin.y,

                                        self.frame.size.width,

                                        self.frame.size.height);

            }

        }];

   }

}

@end

将该类别从工程中移除问题解决

总结:

runtime的使用要慎重,尤其是分类中使用runtime。可能会导致iOS系统bug。并且此类问题很难定位。

上一篇 下一篇

猜你喜欢

热点阅读