91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

自定義tabBar詳解第二種方案

發布時間:2020-08-05 13:51:33 來源:網絡 閱讀:830 作者:yjf123546 欄目:開發技術

自定義tabBar能夠解決自定義圖片無法顯示(只顯示一塊顏色)這個弊端,自定義tabBar要繼承UITabBarController

原圖片:

(1), 自定義tabBar詳解第二種方案 (2), 自定義tabBar詳解第二種方案(3),  自定義tabBar詳解第二種方案(4), 自定義tabBar詳解第二種方案(5)自定義tabBar詳解第二種方案

下面是自定義了一個TabBar ,每個按鈕可以顯示圖片(自定義避免了圖片無法顯示只顯示一塊顏色)

效果圖:


自定義tabBar詳解第二種方案


代碼實現:

一, 首先創建一個繼承UIButton的類CustomTabBar,

在.h文件中:

//指定協議是為了實現點擊button顯示相應地頁面,像系統的tabBar一樣

@protocol CustomTabBarDelegate <NSObject>

-(void)didSelectBarItemAtIndex:(NSInteger)index;

@end

@interface CustomTabBar : UIButton
@property(nonatomic,assign) NSInteger index;
@property (nonatomic,retain) id<CustomTabBarDelegate> delegate;
- (id)initWithFrame:(CGRect)frame WithImage:(UIImage *)p_w_picpath AndSelectedImage:(UIImage *)p_w_picpath;
@end



在.m文件中


#import "CustomTabBar.h"

@interface CustomTabBar ()

@end
@implementation CustomTabBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

    }
    return self;
}

- (id)initWithFrame:(CGRect)frame WithImage:(UIImage *)p_w_picpath AndSelectedImage:(UIImage *)selectedImage{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        [self setAdjustsImageWhenHighlighted:NO];
        [self setImage:p_w_picpath forState:UIControlStateNormal];
        [self setImage:selectedImage forState:UIControlStateSelected];
        [self addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void)click:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(didSelectBarItemAtIndex:)]) {
        [self.delegate didSelectBarItemAtIndex:self.index];
    }
    switch ((int)(btn.selected)) {
        case 0:
            btn.selected = YES;
            break;
        case 1:
            btn.selected = NO;
            break;
        default:
            break;
    }
}


@end


二, 創建一個類CustomizedTabBarController繼承UITabBarController


在.m文件中

- (void)loadViewController
{
    CoverCollectionViewController *coverVC = [[CoverCollectionViewController alloc] init];
    //coverVC.title = @"每日封面";
    CustomizedNavigationController *coverNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:coverVC];
    coverNaVC.navigationItem.title = @"iDailyWATCh";
    
    NewsTableViewController *newsVC = [[NewsTableViewController alloc] init];
    //newsVC.title = @"腕表雜志";
    CustomizedNavigationController *newsNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:newsVC];
    
    BrandsTableViewController *brandsVC = [[BrandsTableViewController alloc] init];
    //brandsVC.title = @"品牌維基";
    CustomizedNavigationController *brandsNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:brandsVC];
    
    VideosCollectionViewController *videosVC = [[VideosCollectionViewController alloc] init];
    //videosVC.title = @"視頻";
    CustomizedNavigationController *videosNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:videosVC];
    
    MoreViewController *moreVC = [[MoreViewController alloc] init];
    //moreVC.title = @"更多";
    CustomizedNavigationController *moreNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:moreVC];
    
    self.viewControllers = @[coverNaVC, newsNaVC, brandsNaVC, videosNaVC, moreNaVC];
    self.selectedIndex = 0;
    RELEASE_SAFE(coverNaVC);
    RELEASE_SAFE(coverVC);
    RELEASE_SAFE(brandsNaVC);
    RELEASE_SAFE(brandsVC);
    RELEASE_SAFE(videosNaVC);
    RELEASE_SAFE(videosVC);
    RELEASE_SAFE(moreNaVC);
    RELEASE_SAFE(moreVC);
}



- (void)setupTabBar
{
    for (int i = 0 ; i < 5; i++) {
        if (i < 3) {
            CustomTabBar *bar = [[CustomTabBar alloc]initWithFrame:CGRectMake(0 + 80 * i, 0, 80, 49) WithImage: [UIImage p_w_picpathNamed:[NSString stringWithFormat:@"%d", i + 100]]AndSelectedImage:[UIImage p_w_picpathNamed:[NSString stringWithFormat:@"%d", i + 130]]];
            bar.index = i;
            bar.delegate = self;
            bar.backgroundColor = RGB(245, 245, 245);
            [self.tabBar addSubview:bar];
            [bar release];
            if (i == 0) {
                bar.selected = YES;
            }
        } else {
            CustomTabBar *bar = [[CustomTabBar alloc]initWithFrame:CGRectMake(0 + 40 * (i + 3), 0, 40, 49) WithImage: [UIImage p_w_picpathNamed:[NSString stringWithFormat:@"%d", i + 100]]AndSelectedImage:[UIImage p_w_picpathNamed:[NSString stringWithFormat:@"%d", i + 130]]];
            bar.index = i;
            bar.delegate = self;
            bar.backgroundColor = RGB(245, 245, 245);
            [self.tabBar addSubview:bar];
            [bar release];
        }
    }
}


-(void)didSelectBarItemAtIndex:(NSInteger)index{
    [self resignBatState];
    self.selectedViewController = self.viewControllers[index];
}
-(void)resignBatState{
    NSArray *arr = [self.tabBar subviews];
    for (CustomTabBar *bar in arr) {
        if ([bar isKindOfClass:[CustomTabBar class]]) {
            bar.selected = NO;
        }
    }
}

- (void)changeViewController:(UIButton *)button
{
    self.selectedIndex = button.tag;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}









向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

景东| 永清县| 金门县| 溧水县| 肇州县| 宣武区| 扎囊县| 漳浦县| 哈密市| 淮北市| 垫江县| 沁水县| 清流县| 雷州市| 台北市| 桐乡市| 利辛县| 荆门市| 九寨沟县| 象山县| 隆安县| 太谷县| 曲阜市| 温泉县| 木兰县| 洮南市| 绩溪县| 扎鲁特旗| 新巴尔虎右旗| 蓝山县| 高密市| 齐河县| 天镇县| 仁怀市| 安徽省| 六盘水市| 沁阳市| 四子王旗| 墨脱县| 兴城市| 马山县|