【问题标题】:UIButton with fixed size custom image具有固定大小自定义图像的 UIButton
【发布时间】:2024-01-18 11:55:01
【问题描述】:

我有一个自定义 UIButton 如下:

 UIButton * action = [UIButton buttonWithType:UIButtonTypeCustom];
        [action setSize:CGSizeMake(30, 30)];
    [action setBackgroundImage:[UIImage imageNamed:@"contextaction.png"] forState:UIControlStateNormal];
    [action setContentMode:UIViewContentModeCenter];
    [action addTarget:self action:@selector(actionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

我拥有的真实图像的大小实际上是 10x10,居中,我希望它保持这种状态,而不是缩放到按钮大小。我该怎么做?

修订代码:

UIButton * action = [UIButton buttonWithType:UIButtonTypeCustom];
    [action setSize:CGSizeMake(44, 44)];
    [action setImage:[UIImage imageNamed:@"contextaction.png"] forState:UIControlStateNormal];
    [action addTarget:self action:@selector(actionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    if (IS_IPAD){
        action.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        action.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    }
     actionButton_ = [[UIBarButtonItem alloc] initWithCustomView:action];

仍在缩放

【问题讨论】:

    标签: iphone objective-c ios ipad uibutton


    【解决方案1】:

    Swift 3 的工作示例希望它有所帮助

    import UIKit
    
    @IBDesignable
    class MenuBtn: UIButton {
    
        convenience init() {
            self.init(frame: CGRect.zero)
        }
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupView()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            setupView()
        }
    
        func setupView(){
            imageView?.contentMode = .scaleAspectFit
            var image = imageView?.image
            image = image?.withRenderingMode(.alwaysTemplate)
    
        }
    
        override var isSelected: Bool {
            didSet {
                tintColor = super.isSelected ? .gray : .gray
            }
        }
    
        override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
            let leftMargin:CGFloat = 40
            let imgWidth:CGFloat = 24
            let imgHeight:CGFloat = 24
            return CGRect(x: leftMargin, y: (contentRect.size.height-imgHeight) * 0.5, width: imgWidth, height: imgHeight)
        }
    
        override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
            let leftMargin:CGFloat = 80
            let rightMargin:CGFloat = 80
            return CGRect(x: leftMargin, y: 0, width: contentRect.size.width-leftMargin-rightMargin, height: contentRect.size.height)
        }
    
       override func backgroundRect(forBounds bounds: CGRect) -> CGRect {
           let leftMargin:CGFloat = 10
           let rightMargin:CGFloat = 10
           let topMargin:CGFloat = 10
           let bottomMargin:CGFloat = 10
           return CGRect(x: leftMargin, y: topMargin, width: bounds.size.width-leftMargin-rightMargin, height: bounds.size.height-topMargin-bottomMargin)
        }
    
    
        override func contentRect(forBounds bounds: CGRect) -> CGRect {
            let leftMargin:CGFloat = 5
            let rightMargin:CGFloat = 5
            let topMargin:CGFloat = 5
            let bottomMargin:CGFloat = 5
            return CGRect(x: leftMargin, y: topMargin, width: bounds.size.width-leftMargin-rightMargin, height: bounds.size.height-topMargin-bottomMargin)
        }
    
        override func prepareForInterfaceBuilder() {
            super.prepareForInterfaceBuilder()
            setupView()
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      包含 QuartzCore 框架并在图层上更改重力。

      action.layer.contentsGravity=kCAGravityCenter;
      

      【讨论】:

        【解决方案3】:

        为此,我认为您需要使用图像插入属性UIButton Class Reference

        【讨论】:

          【解决方案4】:

          使用 setContentMode

          [action setContentMode:UIViewContentModeCenter];
          

          【讨论】:

            【解决方案5】:

            使用UIButtonsetImage:forState:方法代替setBackgroundImage:forState:

            UIButton 继承自UIControl,因此您可以将contentHorizontalAlignmentcontentVerticalAlignment 属性分别设置为UIControlContentHorizontalAlignmentCenterUIControlContentVerticalAlignmentCenter(尽管在大多数情况下这些已经是默认值)。

            【讨论】:

            • 我在使用setImage的时候改了,怎么设置contentHorizo​​ntalAlignment
            • 它仍在缩放到 UIButton 大小
            【解决方案6】:

            可以试试这样的东西...

             [button addSubview:imageView];
            

            其中 imageView 包含图像;

            【讨论】:

              最近更新 更多