iOS runtime獲取類的屬性

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、簡單的應(yīng)用場景:
獲取UIPageControl的屬性,然后修改小原點的的圖片,kvc+runtime。這樣比較方便,當(dāng)然自定義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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,057評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,358評論 4 61
  • 文/靜靜 最近,發(fā)生了一件撞人逃逸,而受害者被二次碾壓死亡的事件。好多人發(fā)文討伐人們的冷漠,我卻想自己是不是也可能...
    靜靜diary閱讀 157評論 0 0
  • 這是一個很具有諷刺性的電影! 《驢得水》被大家認(rèn)為開心麻花繼《夏洛特?zé)馈返牡诙砍晒Φ碾娪白髌?。?dāng)然芒種先生我也...
    芒種先生閱讀 434評論 0 1
  • “孟晨你別光玩手機(jī)了,看看路行嗎?往前怎么走?”駕駛座上的黑發(fā)姑娘略顯緊張的握著方向盤,小心翼翼的減速避開一個橫穿...
    復(fù)明的瞎子閱讀 585評論 0 2

友情鏈接更多精彩內(nèi)容