年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2471|回复: 0

ios6中仿ios7导航按钮

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2014-8-9 23:25:51 | 显示全部楼层 |阅读模式
    #pragma mark 仿iOS7ButtonItem
    - (id)initWithTitle:(NSString *)title addTarget:(id)target action:(SEL)action
    {

        if (self = [super init]){

            // 创建一个普通按钮并设置按钮样式
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:kBlueColor forState:UIControlStateNormal];
            [button setTitleColor:kBlueColorSelected forState:UIControlStateHighlighted];
            [button setTitleColor:kBlueColorDisable forState:UIControlStateDisabled];
            button.titleLabel.font = [UIFont boldSystemFontOfSize:18];
            CGSize size = [button.titleLabel.text sizeWithFont:button.titleLabel.font];
            button.bounds = (CGRect){CGPointZero, size.width + 10, size.height};

            button.contentHorizontalAlignment = iOS7 ? UIControlContentHorizontalAlignmentRight : UIControlContentHorizontalAlignmentLeft;
            // 设置按钮事件处理
            [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

            // 将item初始化为上述按钮样式
            self = [[UIBarButtonItem alloc] initWithCustomView:button];
        }
        return self;
    }

    #pragma mark 仿iOS7返回按钮
    - (id)initWithBackButtonTitle:(NSString *)title addTarget:(id)target action:(SEL)action
    {
        if (self = [super init]){
            // 创建一个普通按钮并设置按钮样式
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

            // 蓝色返回箭头
            UIImage *normal = [UIImage resizeImage:@"navigation_button_back.png"];
            [button setImage:normal forState:UIControlStateNormal];

            // 按钮文字及属性
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:kBlueColor forState:UIControlStateNormal];
            [button setTitleColor:kBlueColorSelected forState:UIControlStateHighlighted];

            // 按钮布局
            button.imageEdgeInsets = UIEdgeInsetsMake(0, iOS7 ? -15 : 4, 0, 0);
            button.titleEdgeInsets = UIEdgeInsetsMake(0, iOS7 ? -10 : 6, 0, 0);

            CGFloat width = [button.titleLabel.text sizeWithFont:button.titleLabel.font].width + 20;
            button.bounds = (CGRect){CGPointZero, width, normal.size.height};

            // 设置按钮事件处理
            [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

            // 将item初始化为上述按钮样式
            self = [[UIBarButtonItem alloc] initWithCustomView:button];
        }
        return self;
    }

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-11-23 18:39 , Processed in 0.045369 second(s), 20 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表