swift UIButton .正常状态异常行为

pu82cl6c  于 2023-06-21  发布在  Swift
关注(0)|答案(1)|浏览(189)

我编程创建按钮:

final class SearchArea: UIView {
    
    private let offset: CGFloat = 5
    
    private(set) lazy var searchRequest = {
        let item = UITextField()
        item.backgroundColor = .systemBackground
        item.borderStyle = .roundedRect
        item.clearButtonMode = .always
        item.layer.cornerRadius = offset
        return item
    }()
    
    private(set) lazy var searchButton = {
        let item = UIButton()
        item.backgroundColor = .systemBlue
        item.layer.cornerRadius = offset
        item.setImage(UIImage(systemName: "magnifyingglass"), for: .normal)
        return item
    }()

    
    func setupView() {
        
        backgroundColor = .systemBackground
        let safeArea = self.safeAreaLayoutGuide
             
                                    
        addSubview(searchButton)
        searchButton.addConstraints(
            .alignTopWithTop(of: safeArea, const: offset),
            .alignBottomWithBottom(of: safeArea, const: offset),
            .alignTrailingWithTrailing(of: safeArea, const: offset),
            .setAspectWidth(ratio: 1)
        )
  
        addSubview(searchRequest)
        searchRequest.addConstraints(
            .alignTopWithTop(of: safeArea, const: offset),
            .alignBottomWithBottom(of: safeArea, const: offset),
            .alignLeadingWithLeading(of: safeArea, const: offset),
            .alignTrailingWithLeading(of: searchButton, const: offset)
        )
 
    }
    
}

但得到一个奇怪的行为-图标显示只有当按钮被按下,所有其他时间它没有图像。但它应该由于苹果手册:
静态变量正常:状态控件的正常或默认状态,在此状态下,控件处于启用状态,但既不被选中也不突出显示。
setImage(UIImage for:.正常状态应该可以正常工作...

q3qa4bjr

q3qa4bjr1#

这是因为按钮有一个默认的色调颜色UIColor.systemBlue,所以不能看到相同背景的图像。尝试区分按钮色调和背景颜色

相关问题