添加右上UIBarButton

来源:互联网 发布:电脑开机进不了windows 编辑:程序博客网 时间:2024/04/29 10:36

- (void)setRightNavigatorItem{

    UIButton *rightBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    rightBtn.frame =CGRectMake(0,0, 40,44);

    [rightBtn setImage:IMAGE(@"add_something.png")forState:UIControlStateNormal];

    [rightBtn addTarget:selfaction:@selector(addSomething)forControlEvents:UIControlEventTouchUpInside];

    

    UIBarButtonItem *negativeSpacer = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpacetarget:nilaction:nil];

    /*

     width为负数时,相当于btn向右移动width数值个像素,由于按钮本身和边界间距为5pix,所以width设为-5时,间距正好调整

     0width为正数时,正好相反,相当于往左移动width数值个像素

     */

    negativeSpacer.width = -10;

    UIBarButtonItem *editButton = [[UIBarButtonItemalloc] initWithCustomView:rightBtn];

    self.navigationItem.rightBarButtonItems =@[negativeSpacer, editButton];

}


原创粉丝点击