gpt4 book ai didi

ios - Swift 按钮无缘无故变蓝

转载 作者:行者123 更新时间:2023-11-28 10:09:17 25 4
gpt4 key购买 nike

我在 swift 中有一个蓝色的按钮,我不知道为什么。我在两个不同的 View Controller 中使用相同的代码,并且出于某种原因,一个是蓝色的。

我对 iOS 开发还是个新手,所以我想知道是否有人对此有所了解?

enter image description here enter image description here

它们都使用相同的功能。并且没有蓝色的 Assets 。我尝试使用不同的图标,但它不是蓝色的。

我还检查了按钮上是否实现了任何颜色更改,但没有调用它。

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 - Swift 按钮无缘无故变蓝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50166864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com