iOS 自定制导航栏 以及左右按钮 方便快捷 直接使用

来源:互联网 发布:淘宝清退中小卖家 编辑:程序博客网 时间:2024/05/22 13:23

#import "superViewController.h"

#define isIos7      ([[[UIDevice currentDevice] systemVersion] floatValue])


#define StatusbarSize ((isIos7 >= 7 && __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1)?20.f:0.f)



- (void)viewDidLoad

{

    [super viewDidLoad];

//判断iOS 系统

    _nSpaceNavY =20;

    if (isIos7 >= 7 &&__IPHONE_OS_VERSION_MAX_ALLOWED >__IPHONE_6_1)

    {

        _nSpaceNavY = 0;

    }


}

//自定制方法

-(void)createNavigationBarWithTitle:(NSString *)title andMenuItem:(UIView * (^)(int index))item

{

    UIImageView *navBar = [[UIImageView alloc]initWithFrame:CGRectMake(0, _nSpaceNavY, CGRectGetWidth(self.view.frame), 64 - _nSpaceNavY)];

    navBar.tag = 98;

    [navBar setBackgroundColor:[UIColor colorWithHexString:@"#de5946"]];

    [self.view addSubview:navBar];

   // [self reloadImage];

    

    

    

    

    /*导航条*/

    _navView = [[UIView alloc] initWithFrame:CGRectMake(0.f, StatusbarSize, CGRectGetWidth(self.view.frame), 44.f)];

    _navView.backgroundColor = [UIColor clearColor];

    [self.view addSubview:_navView];

    _navView.userInteractionEnabled = YES;

    

    

    if (title != nil)

    {

        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((CGRectGetWidth(_navView.frame) - 200)/2, (CGRectGetHeight(_navView.frame) - 40)/2, 200, 40)];

        [titleLabel setText:title];

        [titleLabel setTextAlignment:NSTextAlignmentCenter];

        [titleLabel setTextColor:[UIColor whiteColor]];

        [titleLabel setFont:[UIFont boldSystemFontOfSize:18]];

        [titleLabel setBackgroundColor:[UIColor clearColor]];

        [_navView addSubview:titleLabel];

    }

    

    

    UIView *item1 = item(0);

    if (item1 != nil)

    {

        [_navView addSubview:item1];

    }

    UIView *item2 = item(1);

    if (item2 != nil)

    {

        [_navView addSubview:item2];

    }

    

       

}


//调用示例

    [self createNavigationBarWithTitle:@"编辑课表" andMenuItem:^UIView *(int index) {

       

        if (index == 1)

        {

            

            UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

            [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];

            backButton.frame = CGRectMake(4, 6, 32, 32);

            [backButton addTarget:self action:@selector(backToSuperView) forControlEvents:UIControlEventTouchUpInside];

            return backButton;

            

        }

        

        if (index == 0)

        {

            

            UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

            [backButton setImage:[UIImage imageNamed:@"添加(1)"] forState:UIControlStateNormal];

            backButton.frame = CGRectMake(self.view.frame.size.width-38, 6, 32, 32);

            [backButton addTarget:self action:@selector(submitInformation) forControlEvents:UIControlEventTouchUpInside];

            return backButton;

            

        }

        

        return nil;

    }];



0 0