【问题标题】:BarButtonItems and title not appearing in iOS 11BarButtonItems 和标题未出现在 iOS 11 中
【发布时间】:2017-09-25 10:02:23
【问题描述】:

从 iOS 11 和 Xcode 9 开始,barbuttonitems 和标题不再可见。我是否尝试添加这样的自定义视图并不重要:

let backButton = UIButton.init(frame: CGRect(x: 0, y: 0, width: 180, height: 32))
        backButton.setImage(UIImage(named: "back_icon")?.withRenderingMode(.alwaysTemplate), for: .normal)
        backButton.tintColor = UIColor.white
        backButton.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        backButton.backgroundColor = UIColor.clear
        backButton.titleLabel?.font = UIFont(name: SWMainHelper.sharedInstance.mediumFont, size: 18)
        backButton.setTitleColor(UIColor.white, for: .normal)
        backButton.setTitle("Go back", for: .normal)

        backButton.sizeToFit()

        backButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 0)

        backButton.frame.size.width += 16

        let negativeButtonSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        negativeButtonSpace.width = -16

        self.navigationItem.setLeftBarButtonItems([negativeButtonSpace, UIBarButtonItem(customView: backButton)], animated: true)

或者只是像这样的标准 UIBarButtonItems:

let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
        add.tintColor = UIColor.white
        let play = UIBarButtonItem(title: "Play", style: .plain, target: self, action: #selector(playTapped))
        play.tintColor = UIColor.white
        navigationItem.rightBarButtonItems = [add, play]

使用 Xcode 8 一切正常。

【问题讨论】:

    标签: uibarbuttonitem navigationbar ios11 xcode9


    【解决方案1】:

    使用 Xcode 9 编译时出现此问题,因为现在 UIBarButtonItem 也在使用 autolayput。下面是使它工作的代码。

    UIButton *leftCustomButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 100)];
    
            [leftCustomButton.widthAnchor constraintEqualToConstant:100].active = YES;
            [leftCustomButton.heightAnchor constraintEqualToConstant:35].active = YES;
            [leftCustomButton setTitle:@"TEST" forState:UIControlStateNormal];
            [leftCustomButton.titleLabel setFont:[UIFont boldSystemFontOfSize:16.0]];
            [leftCustomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            UIBarButtonItem * leftButtonItem =[[UIBarButtonItem alloc] initWithCustomView:leftCustomButton];        
            [self.navigationItem setRightBarButtonItems:@[leftButtonItem]];
    

    【讨论】:

      【解决方案2】:

      我在更新到 xCode9 IDE 时遇到了同样的问题。我可以使用UINavigationBar.appearance 解决这个问题:

          let buttonItem = UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self])
          buttonItem.setTitleColor(.black, for: .normal)
          buttonItem.setTitleColor(.gray, for: .disabled)
      

      【讨论】:

      • 很遗憾没有解决我的问题。还是谢谢!
      【解决方案3】:

      我遇到了同样的问题,但以上都没有解决。 我所做的只是在titleView 上设置width,一切正常!

      编辑:

      每个UIViewController 都有一个navigationItem 属性,每个navigationItem 都有一个可选的titleView

      供参考:https://developer.apple.com/documentation/uikit/uinavigationitem/1624935-titleview

      就我而言,我使用的是自定义 titleView,我认为这是问题的原因,因为 Apple 更改了 API 以支持新的导航栏布局。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多