现在的位置: 首页 > 综合 > 正文

兼容ios4及ios5的自定义navigationbar的rightBarButtonItem

2019年10月01日 ⁄ 综合 ⁄ 共 1007字 ⁄ 字号 评论关闭
if ([[[UIDevice currentDevice] systemVersion] intValue] >= 5) {      //for ios5.*
            UIBarButtonItem *rbtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search:)];
            UIImage *rightBarBtn = [[UIImage imageNamed:@"barBtn"] resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 5.0, 0.0, 5.0)];
            [rbtn setBackgroundImage:rightBarBtn forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
            self.navigationItem.rightBarButtonItem = rbtn;
            [rbtn release];
        }
        else {      //for ios4.*
            UIImage *searchImg = [UIImage imageNamed:@"searchImg"];
            UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            searchBtn.frame = CGRectMake(0, 0, 40, 30);
            [searchBtn addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
            [searchBtn setBackgroundImage:searchImg forState:UIControlStateNormal];
            searchBtn.backgroundColor = [UIColor clearColor];
            UIBarButtonItem *searchBarBtn = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
            self.navigationItem.rightBarButtonItem = searchBarBtn;
            [searchBarBtn release];
        }

抱歉!评论已关闭.