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

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];
        }

你可能感兴趣的:(ios,search,action,ios5)