Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 按钮图像大小在BarButtonItem中失真_Swift_Uibutton_Uiimage_Uinavigationbar_Uibarbuttonitem - Fatal编程技术网

Swift 按钮图像大小在BarButtonItem中失真

Swift 按钮图像大小在BarButtonItem中失真,swift,uibutton,uiimage,uinavigationbar,uibarbuttonitem,Swift,Uibutton,Uiimage,Uinavigationbar,Uibarbuttonitem,我想将ui按钮添加到navigationItem.leftBarButtonItem。 我希望ui按钮为24x24px并缩小图像。 但是按钮被扭曲了。。。如果我将UIImage更改为较小的图像,则一切正常。->见图片 除了scaleSpectFit,我还能做什么 let userProfilePic = UIButton() userProfilePic.frame = CGRect(x: 0, y: 0, width: 24, height: 24) userProfi

我想将
ui按钮添加到
navigationItem.leftBarButtonItem
。 我希望
ui按钮
24x24px
并缩小图像。 但是按钮被扭曲了。。。如果我将
UIImage
更改为较小的图像,则一切正常。->见图片

除了
scaleSpectFit
,我还能做什么

    let userProfilePic = UIButton()
    userProfilePic.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
    userProfilePic.setImage(UIImage(named: "profile_icon.jpg"), for: .normal)
    userProfilePic.contentMode = .scaleAspectFit
    userProfilePic.clipsToBounds = true
    userProfilePic.layer.borderWidth = 0.5
    userProfilePic.layer.borderColor = UIColor.white.cgColor
    userProfilePic.layer.cornerRadius = (userProfilePic.frame.size.width) / 2
    userProfilePic.addTarget(self, action: #selector(goToSettings), for: UIControlEvents.touchUpInside)

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: userProfilePic)



谢谢大家!

试试这段代码

问题出现在ios 11+
上,UIBarButtonItem在ios 11+上使用自动布局其他使用框架

     let userProfilePic = UIButton()
        userProfilePic.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
        userProfilePic.setImage(UIImage(named: "profile_icon.jpg"), for: .normal)
        userProfilePic.contentMode = .scaleToFill
        userProfilePic.clipsToBounds = true
        userProfilePic.layer.borderWidth = 0.5
        userProfilePic.layer.borderColor = UIColor.white.cgColor
        userProfilePic.layer.cornerRadius = (userProfilePic.frame.size.width) / 2

      if #available(iOS 11, *) {
    userProfilePic.widthAnchor.constraint(equalToConstant: 24.0).isActive = true
    userProfilePic.heightAnchor.constraint(equalToConstant: 24.0).isActive = true
      }

      userProfilePic.addTarget(self, action: #selector(goToSettings), for: UIControlEvents.touchUpInside)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: userProfilePic)