ios - 导航栏 rightbaritem 图像按钮错误 iOS 11

标签 ios swift ios11 xcode9-beta swift4

此代码在 ios10 中工作正常。我得到我的标签和一个图像按钮,它是用户照片配置文件,圆形圆形..好的。但是当运行 xcode 9 ios11 模拟器时,我得到了它。按钮框架必须是 32x32,当检查 sim 并获取 View 并告诉 xcode 描述 View 时,我得到的输出为 170x32 或类似的东西。

这是我的代码。

let labelbutton = UIButton( type: .system)
    labelbutton.addTarget(self, action:#selector(self.toLogin(_:)), for: .touchUpInside)
    labelbutton.setTitleColor(UIColor.white, for: .normal)
    labelbutton.contentHorizontalAlignment = .right
    labelbutton.titleLabel?.font = UIFont.systemFont(ofSize: 18.00)



    let button = UIButton(type: .custom)
     button.addTarget(self, action:#selector(self.toLogin(_:)), for: .touchUpInside)
     button.frame = CGRect(x: 0, y: 0, width: 32, height: 32)
     button.setTitleColor(UIColor.white, for: .normal)
     button.setTitleColor(UIColor.white, for: .highlighted)


    var buttomItem : UIBarButtonItem = UIBarButtonItem()
    buttomItem.customView = button
    buttomItem.target = self
    buttomItem.action = "ToLogin"

    var labelItem : UIBarButtonItem = UIBarButtonItem()
    labelItem.customView = labelbutton
    labelItem.target = self
    labelItem.action = "ToLogin"


    if let user = PFUser.current() {
        print("LOGIN : checkiando si existe usuario ")
            labelbutton.setTitle(USERNAME, for: UIControlState.normal)
            labelbutton.sizeToFit()

        if(user["profile_photo_url"] != nil) {
            print(" ENCONTRO PROFILE PHOTO URL NOT NIL Y ES \(user["profile_photo_url"])")
            let photoURL = user["profile_photo_url"] as! String
            let a = LoginService.sharedInstance
            a.downloadImage(url: photoURL, complete: { (complete) in

                if (complete) {

                    button.setImage(LoginService.sharedInstance.profile_photo! , for: UIControlState.normal)

                    button.layer.cornerRadius = 0.5 * button.bounds.size.width
                   // button.imageView!.contentMode = .scaleAspectFit
                   // button.imageView!.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
                    //button.imageView!.contentMode = .scaleAspectFit
                    //button.imageView!.clipsToBounds = true
                    //button.imageView!.layer.cornerRadius = 60
                    button.clipsToBounds = true
                    self.NavigationItem.rightBarButtonItems = [buttomItem,labelItem]
                }


            })
        } else {
                self.NavigationItem.rightBarButtonItem = labelItem

        }
            print(" EL FRAME DEL BUTTON ES \(button.frame)")

    } else {

        labelbutton.setTitle("Login", for: UIControlState.normal)
        labelbutton.sizeToFit()
        self.NavigationItem.rightBarButtonItem = labelItem

    }

enter image description here

最佳答案

原因

问题的出现是因为从 ios 11 开始 UIBarButtonItem 使用自动布局而不是处理框架。

解决方案

如果您使用 Xcode 9,您应该为此图像按钮添加宽度限制。

 button.widthAnchor.constraint(equalToConstant: 32.0).isActive = true
 button.heightAnchor.constraint(equalToConstant: 32.0).isActive = true

附言

button 不是 UIBarButtonItem,它是 UIBarButtonItem 中的 UIButton。您不应为 UIBarButtonItem 设置约束,而应为其中的元素设置约束。

关于ios - 导航栏 rightbaritem 图像按钮错误 iOS 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44442573/

相关文章:

objective-c - NSString 和崩溃

ios - iOS 11 不再支持将图案颜色设置为 UITableViewRowAction 的 backgroundColor

objective-c - UIScrollView setContentOffset :animated not working in iOS11

ios - swift : Multiple results from Dictionary in UISearchBar

iOS 根据条件为 UIView 交换类

objective-c - 如何在 swift 中导入 <sys/utsname.h>

ios - 如何在不对每个键使用 if 的情况下解析 json 时捕获错误?

python - 使用 Keras 2 保存的 CoreML 加载模型

ios - 为什么我的 iOS 配置文件是 "Not Trusted"?

ios - 标签栏不显示在导航 Controller 内的 View Controller 上