IOS11 旋转设备时 UIToolbar 左右间距

UIToolbar left and right spacing when rotating device in IOS11

自 iOS11 以来,我在 UIToolbar 的填充方面遇到了一个奇怪的问题。当设备旋转时,工具栏的左右填充会变大(观看下面的示例)。

旋转前设备是纵向模式还是横向模式都没有关系。额外的间距仅在旋转后出现。我认为这是一个自动调整大小的问题。

旋转前查看调试器(正确间距): https://www.dropbox.com/s/1wigv1et88t1mvn/Schermafdruk%202018-01-31%2015.51.05.png?dl=0

旋转后查看调试器(间距错误): https://www.dropbox.com/s/9gnqi6hzv5czcnw/Schermafdruk%202018-01-31%2020.59.48.png?dl=0

示例:

https://www.dropbox.com/s/s7jbmbsuorump5e/spacing-toolbar.gif?dl=0

我正在使用工具栏 class 在工具栏内创建按钮。 在 xcode 界面中,选项 'Autoresize Subview' 被选中。

代码:

class ToolbarClass: UIToolbar {

    //Set height of toolbar
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        var size = super.sizeThatFits(size)
        size.height = 60
        return size
    }

    //Toolbar settings
    override func layoutSubviews() {
        super.layoutSubviews()

        //Default
        self.barStyle = UIBarStyle.default
        self.sizeToFit()

        //Buttons ios11+

        //Space
        let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let spaceBetween:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
        spaceBetween.width = 1.0

        let nameSpace:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
        nameSpace.width = 10

        //Logo
        let logoImage = UIImage(named: "MBS-Logo")
        let logoImageView = UIImageView(image: logoImage)
        logoImageView.frame = CGRect(x: -46, y: 0, width: 48, height: 54)
        logoImageView.contentMode = .scaleAspectFit
        let logoView = UIView(frame: CGRect(x: 0, y: 0, width: 48, height: 54))
        logoView.clipsToBounds = false
        logoView.layer.cornerRadius = logoView.frame.width / 2
        logoView.addSubview(logoImageView)
        let logoImg = UIBarButtonItem(customView: logoView)
        logoImg.customView = logoView

        //Profile
        let profileImage = UIImage(named: "No-Profile")
        let profileImageView = UIImageView(image: profileImage)
        profileImageView.frame = CGRect(x: 40, y: 0, width: 50, height: 50)
        profileImageView.contentMode = .scaleAspectFit
        profileImageView.clipsToBounds = true
        profileImageView.layer.cornerRadius = profileImageView.frame.width / 2
        let profileView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        profileView.clipsToBounds = false
        profileView.addSubview(profileImageView)
        let profileImg = UIBarButtonItem(customView: profileView)
        profileImg.customView = profileView

        //NameLabel
        let nameLbl = UILabel()
        nameLbl.frame = CGRect(x: 0, y: 0, width: 200, height: 60)
        nameLbl.text = "Hi"
        nameLbl.font = UIFont(name: "Roboto", size: 22)
        nameLbl.textColor = UIColor.white
        let nameLabel = UIBarButtonItem()
        nameLabel.customView = nameLbl

        //Settings
        let settingsBtn = UIButton()
        settingsBtn.frame = CGRect(x: 0, y: 0, width: 64, height: 60)
        settingsBtn.setImage(UIImage(named: "Settings-Bar")?.withRenderingMode(.alwaysOriginal), for: .normal)
        settingsBtn.addTarget(self, action: #selector(self.settingsPressed), for: .touchUpInside)
        let settingsButton = UIBarButtonItem()
        settingsButton.customView = settingsBtn

        //Classes
        let classesBtn = UIButton()
        classesBtn.frame = CGRect(x: 0, y: 0, width: 64, height: 60)
        classesBtn.setImage(UIImage(named: "Classes-Bar")?.withRenderingMode(.alwaysOriginal), for: .normal)
        classesBtn.addTarget(self, action: #selector(self.classesPressed), for: .touchUpInside)
        let classesButton = UIBarButtonItem()
        classesButton.customView = classesBtn

        //Set buttons
        self.setItems([profileImg, logoImg, nameSpace, nameLabel, spaceButton, classesButton, spaceBetween, settingsButton], animated: false)

    }

}

首先layoutSubviews不是添加所有项目的正确位置。例如,每次调用 device orientate layoutSubviews 时,将再次创建所有项目。使用 init?(coder:)init(frame:).

将代码移动到 init?(coder:)init(frame:) 后,您将看到左边距和右边距(目前它仅在您定位时出现)。这是 UIToolBar 的实际行为,它会自动在两侧添加边距。

要删除该边距,只需在工具栏项目的开始和结束处添加固定大小的负分隔符。

let negativeFizedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
negativeFizedSpace.width = -20 // Spacing 20 for iPad and 16 for iPhone

class ToolbarClass: UIToolbar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    private func commonInit() {

        //Space
        let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let spaceBetween:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
        spaceBetween.width = 1.0

        let nameSpace:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
        nameSpace.width = 10

        //Logo
        let logoImage = UIImage(named: "MBS-Logo")
        let logoImageView = UIImageView(image: logoImage)
        logoImageView.frame = CGRect(x: -46, y: 0, width: 48, height: 54)
        logoImageView.contentMode = .scaleAspectFit
        let logoView = UIView(frame: CGRect(x: 0, y: 0, width: 48, height: 54))
        logoView.clipsToBounds = false
        logoView.layer.cornerRadius = logoView.frame.width / 2
        logoView.addSubview(logoImageView)
        let logoImg = UIBarButtonItem(customView: logoView)
        logoImg.customView = logoView

        //Profile
        let profileImage = UIImage(named: "No-Profile")
        let profileImageView = UIImageView(image: profileImage)
        profileImageView.frame = CGRect(x: 40, y: 0, width: 50, height: 50)
        profileImageView.contentMode = .scaleAspectFit
        profileImageView.clipsToBounds = true
        profileImageView.layer.cornerRadius = profileImageView.frame.width / 2
        let profileView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        profileView.clipsToBounds = false
        profileView.addSubview(profileImageView)
        let profileImg = UIBarButtonItem(customView: profileView)
        profileImg.customView = profileView

        //NameLabel
        let nameLbl = UILabel()
        nameLbl.frame = CGRect(x: 0, y: 0, width: 200, height: 60)
        nameLbl.text = "Hi"
        nameLbl.font = UIFont(name: "Roboto", size: 22)
        nameLbl.textColor = UIColor.white
        let nameLabel = UIBarButtonItem()
        nameLabel.customView = nameLbl

        //Settings
        let settingsBtn = UIButton()
        settingsBtn.frame = CGRect(x: 0, y: 0, width: 64, height: 60)
        settingsBtn.setImage(UIImage(named: "Settings-Bar")?.withRenderingMode(.alwaysOriginal), for: .normal)
        settingsBtn.addTarget(self, action: #selector(self.settingsPressed), for: .touchUpInside)
        let settingsButton = UIBarButtonItem()
        settingsButton.customView = settingsBtn

        //Classes
        let classesBtn = UIButton()
        classesBtn.frame = CGRect(x: 0, y: 0, width: 64, height: 60)
        classesBtn.setImage(UIImage(named: "Classes-Bar")?.withRenderingMode(.alwaysOriginal), for: .normal)
        classesBtn.addTarget(self, action: #selector(self.classesPressed), for: .touchUpInside)
        let classesButton = UIBarButtonItem()
        classesButton.customView = classesBtn


        let negativeFizedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        negativeFizedSpace.width = -20 // Spacing 20 for iPad and 16 for iPhone

        //Set buttons
        self.setItems([negativeFizedSpace, profileImg, logoImg, nameSpace, nameLabel, spaceButton, classesButton, spaceBetween, settingsButton, negativeFizedSpace], animated: false)
    }

    //Set height of toolbar
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        var size = super.sizeThatFits(size)
        size.height = 60
        return size
    }        
}

更新

如果使用导航控制器工具栏。另一种方法是创建一个 UIViewController 扩展来添加通用工具栏项目,并在 ViewController.

viewDidLoad 方法中调用它
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        addGeneralToolbarItems()
    }
}

class ToolbarClass: UIToolbar {
    //Set height of toolbar
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        var size = super.sizeThatFits(size)
        size.height = 60
        return size
    }
}

extension UIViewController {
    func addGeneralToolbarItems()  {
        //Space
        let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let spaceBetween:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
        spaceBetween.width = 1.0

        let nameSpace:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
        nameSpace.width = 10

        //Logo
        let logoImage = UIImage(named: "MBS-Logo")
        let logoImageView = UIImageView(image: logoImage)
        logoImageView.frame = CGRect(x: -46, y: 0, width: 48, height: 54)
        logoImageView.contentMode = .scaleAspectFit
        let logoView = UIView(frame: CGRect(x: 0, y: 0, width: 48, height: 54))
        logoView.clipsToBounds = false
        logoView.layer.cornerRadius = logoView.frame.width / 2
        logoView.addSubview(logoImageView)
        let logoImg = UIBarButtonItem(customView: logoView)
        logoImg.customView = logoView

        //Profile
        let profileImage = UIImage(named: "No-Profile")
        let profileImageView = UIImageView(image: profileImage)
        profileImageView.frame = CGRect(x: 40, y: 0, width: 50, height: 50)
        profileImageView.contentMode = .scaleAspectFit
        profileImageView.clipsToBounds = true
        profileImageView.layer.cornerRadius = profileImageView.frame.width / 2
        let profileView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        profileView.clipsToBounds = false
        profileView.addSubview(profileImageView)
        let profileImg = UIBarButtonItem(customView: profileView)
        profileImg.customView = profileView

        //NameLabel
        let nameLbl = UILabel()
        nameLbl.frame = CGRect(x: 0, y: 0, width: 200, height: 60)
        nameLbl.text = "Hi"
        nameLbl.font = UIFont(name: "Roboto", size: 22)
        nameLbl.textColor = UIColor.white
        let nameLabel = UIBarButtonItem()
        nameLabel.customView = nameLbl

        //Settings
        let settingsBtn = UIButton()
        settingsBtn.frame = CGRect(x: 0, y: 0, width: 64, height: 60)
        settingsBtn.setImage(UIImage(named: "Settings-Bar")?.withRenderingMode(.alwaysOriginal), for: .normal)
        settingsBtn.addTarget(self, action: #selector(self.settingsPressed), for: .touchUpInside)
        let settingsButton = UIBarButtonItem()
        settingsButton.customView = settingsBtn

        //Classes
        let classesBtn = UIButton()
        classesBtn.frame = CGRect(x: 0, y: 0, width: 64, height: 60)
        classesBtn.setImage(UIImage(named: "Classes-Bar")?.withRenderingMode(.alwaysOriginal), for: .normal)
        classesBtn.addTarget(self, action: #selector(self.classesPressed), for: .touchUpInside)
        let classesButton = UIBarButtonItem()
        classesButton.customView = classesBtn


        let negativeFizedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        negativeFizedSpace.width = -20 // Spacing 20 for iPad and 16 for iPhone

        //Set buttons
        self.setToolbarItems([negativeFizedSpace, profileImg, logoImg, nameSpace, nameLabel, spaceButton, classesButton, spaceBetween, settingsButton, negativeFizedSpace], animated: false)

    }

    @objc func settingsPressed() {

    }

    @objc func classesPressed() {

    }
}