导航栏按钮的偏移

Demo

不多说,直接上

导航栏按钮的偏移_第1张图片
1510820203164.jpg

代码也很简单

    UIButton *settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    //修改按钮向右偏移30 point
    [settingButton setFrame:CGRectMake(20.0, 0.0, 44.0, 44.0)];
    [settingButton setTitle:@"测试" forState:0];
    [settingButton  setTitleColor:[UIColor blueColor] forState:0];
    //修改方法
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 44.0, 44.0)];
    view.backgroundColor = [UIColor redColor];
    [view addSubview:settingButton];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.navigationItem.rightBarButtonItem = rightItem;

你可能感兴趣的:(导航栏按钮的偏移)