我有一个swift的按钮是蓝色的,我不知道为什么。我在两个不同的viewcontrollers中使用相同的代码,其中一个是蓝色的。
我仍然是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)
])
}
答案 0 :(得分:4)
这是因为图像充当模板并呈现按钮色调。 您可以通过使用不同的渲染模式获取图像的副本来阻止这种情况发生:
filterButton.setImage(#imageLiteral(resourceName: "filterIcon").withRenderingMode(.alwaysOriginal), for: .normal)