美文网首页
iOS runtime获取类的属性

iOS runtime获取类的属性

作者: Kx_ | 来源:发表于2017-12-13 18:18 被阅读0次

1、直接上代码:

    unsigned  int count = 0;
    Ivar *members = class_copyIvarList([UIPageControl class], &count);
    
    for (int i = 0; i < count; i++)
    {
        Ivar var = members[i];
        const char *memberAddress = ivar_getName(var);
        const char  *memberType = ivar_getTypeEncoding(var);
        NSLog(@"address = %s ; type = %s",memberAddress,memberType);
    }

log:

address = _lastUserInterfaceIdiom ; type = q
address = _indicators ; type = @"NSMutableArray"
address = _currentPage ; type = q
address = _displayedPage ; type = q
address = _pageControlFlags ; type = {?="hideForSinglePage"b1"defersCurrentPageDisplay"b1}
address = _currentPageImage ; type = @"UIImage"
address = _pageImage ; type = @"UIImage"
address = _currentPageImages ; type = @"NSMutableArray"
address = _pageImages ; type = @"NSMutableArray"
address = _backgroundVisualEffectView ; type = @"UIVisualEffectView"
address = _currentPageIndicatorTintColor ; type = @"UIColor"
address = _pageIndicatorTintColor ; type = @"UIColor"
address = _legibilitySettings ; type = @"_UILegibilitySettings"
address = _numberOfPages ; type = q

2、简单的应用场景:
获取UIPageControl的属性,然后修改小原点的的图片,kvc+runtime。这样比较方便,当然自定义UIPageControl也是可以的。

    UIImage *image = [UIImage imageNamed:@"line1"];
    UIImage *image2 = [UIImage imageNamed:@"line2"];
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 100, 400, 20)];
    pageControl.numberOfPages = 5;
    pageControl.currentPage = 0;
    [pageControl setValue:image forKey:@"_pageImage"];
    [pageControl setValue:image2 forKey:@"_currentPageImage"];
    [self.view addSubview:pageControl];

效果:

之前: image.png 之后: image.png

相关文章

网友评论

      本文标题:iOS runtime获取类的属性

      本文链接:https://www.haomeiwen.com/subject/wdezixtx.html