How to adjust UIToolBar left and right padding

后端 未结 10 531
自闭症患者
自闭症患者 2020-12-02 05:05

I create one UIToolbar with code and another with interface builder. But, found out the two toolbar having different left and right padding which shown below:

From I

10条回答
  •  抹茶落季
    2020-12-02 05:38

    I met the same issue. Finally, I solved this by subclassing UIToolbar and override the layout subviews method.

    - (void)layoutSubviews {
    
      [super layoutSubviews];
    
      if (leftItem_ && leftItem_.customView
          && [leftItem_.customView isKindOfClass:[UIButton class]]) {
        CGRect newFrame = leftItem_.customView.frame;
        newFrame.origin.x = 0;   // reset the original point x to 0; default is 12, wired number
        leftItem_.customView.frame = newFrame;    
      }
    
      if (rightItem_ && rightItem_.customView
          && [rightItem_.customView isKindOfClass:[UIButton class]]) {
        CGRect newFrame = rightItem_.customView.frame;
        newFrame.origin.x = self.frame.size.width - CGRectGetWidth(newFrame);
        rightItem_.customView.frame = newFrame;
      }
    
    }
    

提交回复
热议问题