内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

UIkit框架(14)自定義標(biāo)簽控制器-創(chuàng)新互聯(lián)

  • 自定義UITabBarController子類(lèi)

    創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供臨夏企業(yè)網(wǎng)站建設(shè),專(zhuān)注與網(wǎng)站制作、做網(wǎng)站、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為臨夏眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。

UITabBarController的tabBar往往不能滿(mǎn)足我們的需求

通過(guò)自定義UITabBarController子類(lèi)自定義標(biāo)簽欄是經(jīng)常采用的方式。

基本步驟:

  1)定義UIButton子類(lèi)作為標(biāo)簽按鈕

    2)定義模型類(lèi),管理每個(gè)頁(yè)面的控制器以及對(duì)應(yīng)標(biāo)簽欄上的數(shù)據(jù)

    3)自定義標(biāo)簽欄

    4)定義UITabBarController子類(lèi)

  • 定義標(biāo)簽按鈕

添加兩種創(chuàng)建方法:只顯示圖片和文字圖片都顯示的

+ (AMTabBarButton *)tabBarButtonWithTitle:(NSString *)title normalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage
{
    AMTabBarButton * btn = [AMTabBarButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:normalImage forState:UIControlStateNormal];
    [btn setImage:selectedImage forState:UIControlStateSelected];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    btn.titleLabel.font = [UIFont systemFontOfSize:10];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    return btn;
}
+ (AMTabBarButton *)tabBarButtonWithNormalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage
{
    AMTabBarButton * btn = [AMTabBarButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:normalImage forState:UIControlStateNormal];
    [btn setImage:selectedImage forState:UIControlStateSelected];
    [btn setTitle:nil forState:UIControlStateNormal];
    return btn;
}

修改按鈕內(nèi)部label和p_w_picpathView的位置,重寫(xiě)以下方法即可

- (CGRect)titleRectForContentRect:(CGRect)contentRect
- (CGRect)p_w_picpathRectForContentRect:(CGRect)contentRect

如:

- (CGRect)p_w_picpathRectForContentRect:(CGRect)contentRect
{
    CGFloat x, y, w, h;
  
    if ( [self titleForState:UIControlStateNormal] == nil ) {
        w = h = contentRect.size.height*0.8;
    }
    else {
        w = h = contentRect.size.height*0.45;
    }
    x = contentRect.size.width/2 - w/2;
    y = contentRect.size.height*0.1;
    return CGRectMake(x, y, w, h);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat x, y, w, h;
    if ( [self titleForState:UIControlStateNormal] == nil ) {
        return CGRectZero;
    }
    h = contentRect.size.height*0.25;
    w = contentRect.size.width*0.6;
    x = contentRect.size.width*0.2;
    y = contentRect.size.height*0.65;
    return CGRectMake(x, y, w, h);
}

取消按鈕點(diǎn)擊時(shí)的高亮效果:重寫(xiě)highlighted屬性的setter方法

- (void)setHighlighted:(BOOL)highlighted {    
}

  • 定義模型類(lèi)

管理每個(gè)子控制器及標(biāo)簽欄上按鈕的數(shù)據(jù)

@interface AMTabBarItemModel : NSObject

@property (nonatomic, copy) UIImage * normalImage;

@property (nonatomic, copy) UIImage * selectedImage;

@property (nonatomic, copy) NSString * title;

@property (nonatomic, strong) UIViewController * viewController;

+ (instancetype) tabBarItemModelWithNormalImage:(UIImage*) normalImage selectedImage:(UIImage*) selectedImage title:(NSString*) title viewController:(UIViewController*) viewController;

@end

  • 定義標(biāo)簽欄

定義一個(gè)UIView的子類(lèi)作為自定義的標(biāo)簽類(lèi)

設(shè)置樣式屬性,兩種樣式:只有圖片和文字圖片都有的

typedef enum {
    AMTabBarStyleTitleAndImage,
    AMTabBarStyleImageOnly
}AMTabBarStyle;

創(chuàng)建方法:

+ (instancetype)tabBarViewWithItemModels:(NSArray *)models tabBarStyle:(AMTabBarStyle)tabBarStyle
{
    return [[self alloc] initWithItemModels:models tabBarStyle:tabBarStyle];
}
- (instancetype) initWithItemModels:(NSArray*) models tabBarStyle:(AMTabBarStyle) tabBarStyle
{
    if ( self = [super init] ) {
        self.tabBarStyle = tabBarStyle;
        int i = 1;
        for ( AMTabBarItemModel * model in models ) {
            AMTabBarButton * btn;
            if ( tabBarStyle == AMTabBarStyleTitleAndImage ) {
                btn = [AMTabBarButton tabBarButtonWithTitle:model.title normalImage:model.normalImage selectedImage:model.selectedImage];
            }
            else {
                btn = [AMTabBarButton tabBarButtonWithNormalImage:model.normalImage selectedImage:model.selectedImage];
            }
            
            [self addSubview:btn];
            
            [btn addTarget:self action:@selector(itemBtnClicked:) forControlEvents:UIControlEventTouchDown];
            btn.tag = i;
            i++;
        }
        self.selectedBtn = self.subviews[0];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat x, y, w, h;
    h = self.frame.size.height;
    w = self.frame.size.width/self.subviews.count;
    y = x = 0;
    
    for ( AMTabBarButton * btn in self.subviews ) {
        btn.frame = CGRectMake(x, y, w, h);
        x += w;
    }
}

   參數(shù)items:模型對(duì)象數(shù)組

當(dāng)標(biāo)簽按鈕被點(diǎn)擊時(shí)發(fā)送代理方法:

- (void) itemBtnClicked:(AMTabBarButton*) sender
{
    self.selectedBtn = sender;
    if ( self.delegate && [self.delegate respondsToSelector:@selector(tabBarView:selectedIndex:)]) {
        [self.delegate tabBarView:self selectedIndex:sender.tag];
    }
}
- (void) setSelectedBtn:(AMTabBarButton *)selectedBtn
{
    if ( _selectedBtn != selectedBtn ) {
        if ( _selectedBtn != nil ) {
            _selectedBtn.selected = NO;
        }
        _selectedBtn = selectedBtn;
        _selectedBtn.selected = YES;
    }
}

    其中selectBtn屬性表示當(dāng)前被選擇的按鈕

  • 定義UITabBarController子類(lèi)

添加屬性:樣式、標(biāo)簽欄、模型數(shù)組

@property (nonatomic, assign) AMTabBarStyle tabBarStyle;
@property (nonatomic, weak) AMTabBarView * myTabBar;
@property (nonatomic, strong) NSArray * items;

添加創(chuàng)建控制器子類(lèi)的方法:

+ (instancetype)tabBarControllerWithItems:(NSArray *)items tabBarStyle:(AMTabBarStyle)tabBarStyle
{
    AMTabBarController * tabBarController = [[UIStoryboard storyboardWithName:@"AMTabBarController" bundle:nil] instantiateInitialViewController];
    
    tabBarController.items = items;
    tabBarController.tabBarStyle = tabBarStyle;
    
    NSMutableArray * arr = [NSMutableArray array];
    
    for ( AMTabBarItemModel * model in items ) {
        [arr addObject:model.viewController];
        model.viewController = nil;
    }
    tabBarController.viewControllers = [arr copy];
    
    return tabBarController;
}

創(chuàng)建自定義的標(biāo)簽欄,添加到tabBar視圖上

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupTabBar];
}
- (void) setupTabBar
{
    AMTabBarView * myTabBar = [AMTabBarView tabBarViewWithItemModels:self.items tabBarStyle:self.tabBarStyle];
    
    self.myTabBar = myTabBar;
    
    [self.tabBar addSubview:self.myTabBar];
    self.myTabBar.delegate = self;
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    self.myTabBar.frame = self.tabBar.bounds;
}

實(shí)現(xiàn)自定義標(biāo)簽欄的代理方法,切換頁(yè)面

- (void)tabBarView:(AMTabBarView *)tabBarView selectedIndex:(NSInteger)index
{
    self.selectedIndex = index-1;
}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

名稱(chēng)欄目:UIkit框架(14)自定義標(biāo)簽控制器-創(chuàng)新互聯(lián)
文章起源:http://www.rwnh.cn/article22/cceejc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、動(dòng)態(tài)網(wǎng)站網(wǎng)站改版、靜態(tài)網(wǎng)站、標(biāo)簽優(yōu)化、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
舟曲县| 霸州市| 梓潼县| 偏关县| 泽州县| 浦江县| 托克托县| 南阳市| 无棣县| 游戏| 赣州市| 兴国县| 四平市| 嘉善县| 微山县| 察雅县| 莱阳市| 石楼县| 高邑县| 渭南市| 贺州市| 紫阳县| 霍林郭勒市| 屯昌县| 崇礼县| 台北市| 青冈县| 丰镇市| 会泽县| 丰都县| 西贡区| 依兰县| 南岸区| 仁怀市| 滕州市| 双城市| 合作市| 巴青县| 正镶白旗| 咸宁市| 咸丰县|