自定义导航栏左边按钮的位置靠左问题,右边按钮的位置靠右的问题

    UIButton *leftBtn = [[UIButton   alloc]  initWithFrame:CGRectMake(-10,0,scrrenW*0.05,44)];

    [leftBtn setBackgroundImage:[UIImage  imageNamed:@"btnBack"]   forState:UIControlStateNormal];

    leftBtn.contentHorizontalAlignmentUIControlContentHorizontalAlignmentLeft;

    [leftBtn setTitleColor:totalColorforState:UIControlStateNormal];

    leftBtn.titleLabel.font = [UIFont  systemFontOfSize:40];

    [leftBtn addTarget:self  action:@selector(backClick)  forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *item = [[UIBarButtonItem   alloc]   initWithCustomView:leftBtn];

    UIBarButtonItem *negativeSpacer1 = [[UIBarButtonItem   alloc ] initWithBarButtonSystemItem :UIBarButtonSystemItemFixedSpace

                                 target :nil  action :nil ];

    

    negativeSpacer1.width = -20 ;//这个数值可以根据情况自由变化

    

    self.navigationItem.leftBarButtonItems =@[negativeSpacer, item] ;// 解决左边按钮靠左的问题,****右边按钮可以用同样的方法解决


你可能感兴趣的:(iOS,控件)