【问题标题】:How to set circle button with background image on right navigation bar item如何在右侧导航栏项目上设置带有背景图像的圆形按钮
【发布时间】:2016-06-15 02:38:08
【问题描述】:

我尝试将圆形按钮放在 iOS 的右侧导航栏上,但不幸的是,当我使用按钮背景时,它不会使图像变圆,而是显示方形背景图像,但是当我删除图像并将背景颜色放在按钮周围时背景色。 我试过的代码:

        let button = UIButton()
        button.frame = CGRectMake(0, 0, 40, 40)
        button.layer.cornerRadius = 0.5 * button.bounds.size.width
        button.setImage(self.myPic, forState: .Normal)            
        let barButton = UIBarButtonItem()
        barButton.customView = button
        self.navigationItem.rightBarButtonItem = barButton

【问题讨论】:

    标签: ios swift uibutton uibarbuttonitem


    【解决方案1】:

    尝试使用此代码.. 对于带有图像的圆形按钮 -

    let button = UIButton()
    button.frame = CGRectMake(0, 0, 40, 40)
    let color = UIColor(patternImage: UIImage(named: "btnImage")!)
    button.backgroundColor = color
    button.layer.cornerRadius = 0.5 * button.bounds.size.width
    let barButton = UIBarButtonItem()
    barButton.customView = button
    self.navigationItem.rightBarButtonItem = barButton
    

    有实物图---

       let button = UIButton()
        button.frame = CGRectMake(0, 0, 40, 40)
    
        let image = UIImage(named: "btnImage")!
    
        UIGraphicsBeginImageContextWithOptions(button.frame.size, false, image.scale)
        let rect  = CGRectMake(0, 0, button.frame.size.width, button.frame.size.height)
        UIBezierPath(roundedRect: rect, cornerRadius: rect.width/2).addClip()
        image.drawInRect(rect)
    
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
    
        let color = UIColor(patternImage: newImage)
        button.backgroundColor = color
        button.layer.cornerRadius = 0.5 * button.bounds.size.width
        let barButton = UIBarButtonItem()
        barButton.customView = button
        self.navigationItem.rightBarButtonItem = barButton
    

    【讨论】:

    • 基本上你应该用图像创建一个颜色来设置,然后设置按钮的背景颜色
    • 它可以工作,但这也会裁剪图像。我不想裁剪我的图像
    • 我有这个解决方案。我编辑了我的答案。如果这个或我之前的解决方案解决了您的问题,请将此作为答案进行检查
    【解决方案2】:

    我为 Swift 4 做了一个解决方案,你也需要调整图像和框架的大小

    let avatarSize: CGFloat = 30
    let button = UIButton()
    button.frame = CGRect(x: 0, y: 0, width: avatarSize, height: avatarSize)
    button.setImage(UIImage(named: "avatar")?.resizeImage(avatarSize, opaque: false), for: .normal)
    
    if let buttonImageView = button.imageView {
        button.imageView?.layer.cornerRadius = buttonImageView.frame.size.width / 2
        button.imageView?.clipsToBounds = true
        button.imageView?.contentMode = .scaleAspectFit
    }
    

    你需要的扩展:

    extension UIImage {
        func resizeImage(_ dimension: CGFloat, opaque: Bool, contentMode: 
            UIViewContentMode = .scaleAspectFit) -> UIImage {
            var width: CGFloat
            var height: CGFloat
            var newImage: UIImage
    
            let size = self.size
            let aspectRatio =  size.width/size.height
    
            switch contentMode {
            case .scaleAspectFit:
                if aspectRatio > 1 {                            // Landscape image
                    width = dimension
                    height = dimension / aspectRatio
                } else {                                        // Portrait image
                    height = dimension
                    width = dimension * aspectRatio
                }
    
            default:
                fatalError("UIIMage.resizeToFit(): FATAL: Unimplemented ContentMode")
            }
    
            if #available(iOS 10.0, *) {
                let renderFormat = UIGraphicsImageRendererFormat.default()
                renderFormat.opaque = opaque
                let renderer = UIGraphicsImageRenderer(size: CGSize(width: width, height: height), format: renderFormat)
                newImage = renderer.image {
                    (context) in
                    self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
                }
            } else {
                UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), opaque, 0)
                self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
                newImage = UIGraphicsGetImageFromCurrentImageContext()!
                UIGraphicsEndImageContext()
            }
    
            return newImage
        }
    }
    

    【讨论】:

    • 通过在接受的答案中使用这种方法而不是 UIColor(patternImage: newImage),图像具有更好的质量。这里唯一的问题是我想获得圆形图像(或方形 - 没关系),而是根据图像比例获得矩形图像。所以我现在正在尝试结合这个答案并接受一个。
    【解决方案3】:

    迅速 2 在圆圈中设置栏按钮

    func setProfileImageOnBarButton() {
    
        let button = UIButton()
        button.setImage(profileImage, forState: UIControlState.Normal)
        button.addTarget(self, action:#selector(self.openUserProfile), forControlEvents: UIControlEvents.TouchUpInside)
        button.frame = CGRectMake(0, 0, 36, 36)
        button.layer.cornerRadius = CGRectGetWidth(button.frame) / 2
        button.layer.masksToBounds = true
        let barButton = UIBarButtonItem(customView: button)
        self.navigationItem.rightBarButtonItem = barButton
    
    }
    

    【讨论】:

    • CGRectMake 在 swift 3 中不存在。
    • 它只是 swift3 上的 CGRect
    【解决方案4】:

    Swift 5 - 你可以简单地做到这一点,并确保你将 clipstobounds 设置为 true

    teacherImage.setImage(UIImage(named: "icon_profile"), for: .normal)
    teacherImage.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
    teacherImage.addTarget(self, action: #selector(addPressed), for: .touchUpInside)
    teacherImage.layer.cornerRadius = 0.5 * teacherImage.bounds.size.width
    teacherImage.clipsToBounds = true
    let rightNavBarButton = UIBarButtonItem(customView: teacherImage)
    let currWidth = rightNavBarButton.customView?.widthAnchor.constraint(equalToConstant: 40)
    currWidth?.isActive = true
    let currHeight = rightNavBarButton.customView?.heightAnchor.constraint(equalToConstant: 40)
    currHeight?.isActive = true
    self.navigationItem.rightBarButtonItem = rightNavBarButton
    

    【讨论】:

      【解决方案5】:

      它适用于 Objective-C。试一试!

      UIButton *avatarButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
      // redraw the image to fit new size
      UIGraphicsBeginImageContextWithOptions(avatarButton.frame.size, NO, 0);
      [[UIImage imageNamed:@"pikachu"] drawInRect:CGRectMake(0, 0, avatarButton.frame.size.width, avatarButton.frame.size.height)];
      UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      
      UIColor *color = [UIColor colorWithPatternImage: resultImage];
      avatarButton.backgroundColor = color;
      avatarButton.layer.cornerRadius = 0.5 * avatarButton.bounds.size.width;
      UIBarButtonItem *barButton = UIBarButtonItem.new;
      barButton.customView = avatarButton;
      self.navigationItem.rightBarButtonItem = barButton;
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      相关资源
      最近更新 更多