我有一个蓝色的雨燕按钮,我不知道为什么。我在两个不同的viewcontroller中使用相同的代码,其中一个由于某种原因是蓝色的。
我对iOS的开发还不熟悉,所以我想知道是否有人知道这件事?
他们都在使用相同的功能。没有蓝色资产。我用了不同的图标,但不是蓝色的。
我还检查了一下按钮上是否实现了任何颜色更改,但它没有调用。
private func prepareSearchView() {
searchShadowView.backgroundColor = .white
searchShadowView.layer.shadowColor = UIColor.black.cgColor
searchShadowView.layer.shadowOffset = CGSize(width: 1, height: 1)
searchShadowView.layer.shadowOpacity = 0.3
searchShadowView.layer.shadowRadius = 2
searchShadowView.layer.masksToBounds = false
searchShadowView.layer.cornerRadius = 2
view.addSubview(searchShadowView)
view.bringSubview(toFront: searchShadowView)
searchShadowView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
searchShadowView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 20),
searchShadowView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
searchShadowView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
searchShadowView.heightAnchor.constraint(equalToConstant: searchViewHeight)
])
searchView.backgroundColor = .white
searchView.layer.borderWidth = 1
searchView.layer.borderColor = UIColor.segmentedControlBackgroundGray.cgColor
view.addSubview(searchView)
view.bringSubview(toFront: searchView)
searchView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
searchView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 20),
searchView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
searchView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
searchView.heightAnchor.constraint(equalToConstant: searchViewHeight)
])
let glassImageView = UIImageView(image: #imageLiteral(resourceName: "glassIcon"))
searchView.addSubview(glassImageView)
glassImageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
glassImageView.topAnchor.constraint(equalTo: searchView.topAnchor, constant: 14),
glassImageView.leadingAnchor.constraint(equalTo: searchView.leadingAnchor, constant: 20),
glassImageView.bottomAnchor.constraint(equalTo: searchView.bottomAnchor, constant: -14),
glassImageView.widthAnchor.constraint(equalToConstant: 19)
])
let searchButton = UIButton(type: .custom)
searchButton.setTitle("Where do you want to eat?", for: .normal)
searchButton.setTitleColor(.placeHolderGray, for: .normal)
searchButton.titleLabel?.font = UIFont.mediumFont(ofSize: 15.0)
searchButton.titleLabel?.adjustsFontSizeToFitWidth = true
searchButton.addTarget(self, action: #selector(searchButtonPressed), for: .touchUpInside)
searchView.addSubview(searchButton)
searchButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
searchButton.topAnchor.constraint(equalTo: searchView.topAnchor),
searchButton.leadingAnchor.constraint(equalTo: glassImageView.trailingAnchor, constant: 10),
searchButton.heightAnchor.constraint(equalTo: searchView.heightAnchor),
])
filterButton.setImage(#imageLiteral(resourceName: "filterIcon"), for: .normal)
filterButton.addTarget(self, action: #selector(filterButtonPressed), for: .touchUpInside)
searchView.addSubview(filterButton)
filterButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
filterButton.topAnchor.constraint(equalTo: searchView.topAnchor),
filterButton.leadingAnchor.constraint(equalTo: searchButton.trailingAnchor, constant: 10),
filterButton.bottomAnchor.constraint(equalTo: searchView.bottomAnchor),
filterButton.trailingAnchor.constraint(equalTo: searchView.trailingAnchor, constant: -20),
filterButton.widthAnchor.constraint(equalToConstant: 25)
])
}
最佳答案
这是由于图像作为模板,并采取按钮的色彩。
可以通过使用不同的渲染模式复制图像来阻止这种情况的发生:
filterButton.setImage(#imageLiteral(resourceName: "filterIcon").withRenderingMode(.alwaysOriginal), for: .normal)
关于ios - 快捷按钮无故变成蓝色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50166864/