iOS 11.0版本及以上 UITextField 出现内存泄漏 造成cpu暴涨 解决方案

原因

   UITextField有一个名为_textContentView的私有变量,这个私有变量有一个名为_provider的私有变量,这个_provider就是UITextField自己。这些私有变量都是被强引用的,因此在使用当中引起了循环引用。

解决方案

   添加一个UITextField 的扩展类 放入以下代码,再使用UITextField 的地方添加头文件或者是在pch文件中调用,即可

- (void)didMoveToWindow{

[super didMoveToWindow];

if (@available(iOS 11.0, *)) {

    NSString *keyPath = @"textContentView.provider";

    @try {

        id provider = [self valueForKey:keyPath];

        if (!provider && self) {

            [self setValue:self forKey:keyPath];

        }else{

            [self setValue:nil forKey:keyPath];

        }

    } @catch (NSException *exception) {

        NSLog(@"%@", exception);

    } @finally {


    }

}

---------------------

作者:疯狐狸12138

来源:CSDN

原文:https://blog.csdn.net/sinat_23907467/article/details/83856608

版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(iOS 11.0版本及以上 UITextField 出现内存泄漏 造成cpu暴涨 解决方案)