影院項目總結(jié)

三級控制器搭建都大同小異,這次的項目是使用storyboard搭建, 注意下在移除按鈕的時候,那個方法應(yīng)該寫在viewWillAppear,而且一定要調(diào)用父類方法

界面.png

自定義NavigationController, 注意下設(shè)置背景圖片和設(shè)置字體顏色

#import "WXNavigationController.h"

@interface WXNavigationController ()

@end

@implementation WXNavigationController

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // 設(shè)置背景圖片
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg_all-64"] forBarMetrics:UIBarMetricsDefault];
    
    // 設(shè)置字體顏色
    NSDictionary *dict = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:15],
                           NSForegroundColorAttributeName : [UIColor whiteColor]};
    [self.navigationBar setTitleTextAttributes:dict];
}

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


@end

自定義TabBarController, 注意下tag值

#import "WXTabBarController.h"
#import "WXTabBarButton.h"

@interface WXTabBarController ()

/** 選中圖片 */
@property (nonatomic, strong) UIImageView *selectdImage;

@end

@implementation WXTabBarController

// 這里要注意:因為storyboard創(chuàng)建出來的界面后顯示,所有移除的方法應(yīng)該寫在這
- (void)viewWillAppear:(BOOL)animated {
    // 一定要記得調(diào)用父類方法
    [super viewWillAppear:animated];
    
    // 1.移除系統(tǒng)上的button
    [self removeSystemBtn];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 2.用自己的button
    [self createTabBarBtn];
    
}

#pragma mark - 移除系統(tǒng)上的button
- (void)removeSystemBtn {
    
    for (UIView *view in self.tabBar.subviews) {
        
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {

            [view removeFromSuperview];
            
        }
    }
    
}

#pragma mark - 用自己的button
- (void)createTabBarBtn {
    
    // 1.標題
    NSArray *titles = @[@"首頁", @"新聞", @"Top", @"影院", @"更多"];
    
    // 2.圖片
    NSArray *imageNames = @[@"movie_home", @"msg_new", @"start_top250", @"icon_cinema", @"more_setting"];
    
    // 3.按鈕寬度
    CGFloat btnWidth = [UIScreen mainScreen].bounds.size.width / imageNames.count;
    
    // 4.背景圖片
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tab_bg_all"];
    
    // 5.選中圖片
    self.selectdImage = [[UIImageView alloc] init];
    self.selectdImage.frame = CGRectMake(0, 0, btnWidth, 49);
    self.selectdImage.image = [UIImage imageNamed:@"selectTabbar_bg_all"];
    [self.tabBar addSubview:self.selectdImage];
    
    for (NSInteger i = 0; i < imageNames.count; i++) {
        
        CGRect btnRect = CGRectMake(i * btnWidth, 0, btnWidth, 49);
        
        WXTabBarButton *button = [[WXTabBarButton alloc] initWithFrame:btnRect withTitle:titles[i] withImage:imageNames[i]];

        
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        button.tag = 1000 + i;

        [self.tabBar addSubview:button];
        
    }

}

#pragma mark - 切換按鈕事件
- (void)buttonAction:(UIButton *)button {
    
    self.selectedIndex = button.tag - 1000;
    
    self.selectdImage.center = button.center;
    
}

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


@end

自定義button繼承UIControl, 沒什么好講的,注意下它們的位置

- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title withImage:(NSString *)image {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        // 1.創(chuàng)建image
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width - 20) / 2, 5, 20, 20)];
        imageView.image = [UIImage imageNamed:image];
        // 圖片填充類型
        imageView.contentMode = UIViewContentModeScaleAspectFit;

        // 2.創(chuàng)建label
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 20, self.bounds.size.width, 20)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:12];
        titleLabel.text = title;
        titleLabel.textColor = [UIColor whiteColor];
        
        [self addSubview:imageView];
        [self addSubview:titleLabel];
    }
    
    return self;
}
最后編輯于
?著作權(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)容

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