我在 iOS 15 中遇到了一個問題,將影像設定到右上角時不起作用,而在 iOS 15 之前它可以正常作業。
我想要達到的目標:

我正在實作的目標:

這是我用于 iOS 15 的完整代碼:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let btnDropdown = getDropdownButton()
btnDropdown.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(btnDropdown)
NSLayoutConstraint.activate([
btnDropdown.heightAnchor.constraint(equalToConstant: 44),
btnDropdown.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
btnDropdown.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
btnDropdown.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: 30)
])
}
fileprivate func getDropdownButton() -> UIButton {
var button: UIButton!
if #available(iOS 15.0, *),
let btnConfig = getButtonConfiguration() {
button = UIButton(configuration: btnConfig)
} else {
button = UIButton(type: .custom)
}
button.setTitle("Select Tags", for: .normal)
setupGeneralSettings(for: button)
return button
}
fileprivate func setupGeneralSettings(for button: UIButton) {
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
button.contentHorizontalAlignment = .left
button.layer.borderWidth = 1
button.layer.cornerRadius = 8
button.clipsToBounds = true
button.backgroundColor = .lightGray.withAlphaComponent(0.3)
button.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
button.setTitleColor(UIColor.black, for: .normal)
}
@available(iOS 15.0, *)
fileprivate func getButtonConfiguration() -> UIButton.Configuration? {
var btnConfig = UIButton.Configuration.plain()
btnConfig.buttonSize = .medium
btnConfig.titleAlignment = .leading
btnConfig.imagePlacement = .trailing
btnConfig.image = UIImage(systemName: "arrowtriangle.down.circle")
btnConfig.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8)
btnConfig.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = UIFont.systemFont(ofSize: 14, weight: .medium)
return outgoing
}
return btnConfig
}
}
注意: 當我說 iOS 15 時,我的意思是我正在使用 UIButton.Configuration,因為 UIButton 的 titleEdgeInset、imageEdgeInset 和 contentEdgeInset 在 iOS 15 中已被棄用。另外,我認為 UIButton.Configuration 中缺少一些東西影像將到達按鈕的右邊緣,但我不確定它是什么。
請讓我知道是否需要任何其他資訊。
提前致謝!
uj5u.com熱心網友回復:
通過將一些舊式按鈕代碼與新式混合使用,您可能會遇到問題,但是.......configuration
更改此行setupGeneralSettings():
button.contentHorizontalAlignment = .left
對此:
button.contentHorizontalAlignment = .fill
看看你是否得到了想要的布局。
uj5u.com熱心網友回復:
您可以像這樣添加外部影像:
在您的控制器類下,宣告您的物件:
let btnDropdown = UIButton()
let myImageView = UIImageView()
在 viewDidLoad(或函式中)設定物件屬性和約束:
myImageView.image = UIImage(systemName: "arrowtriangle.down.circle")
myImageView.tintColor = .systemBlue
myImageView.contentMode = .scaleAspectFit
myImageView.clipsToBounds = true
myImageView.translatesAutoresizingMaskIntoConstraints = false
btnDropdown.contentHorizontalAlignment = .left
btnDropdown.setTitle("Select Tags", for: .normal)
btnDropdown.layer.borderWidth = 1
btnDropdown.layer.borderColor = UIColor.black.cgColor // here set your border color
btnDropdown.layer.cornerRadius = 8
btnDropdown.clipsToBounds = true
btnDropdown.setTitleColor(UIColor.black, for: .normal)
btnDropdown.translatesAutoresizingMaskIntoConstraints = false
btnDropdown.configuration = UIButton.Configuration.filled()
btnDropdown.configuration?.background.backgroundColor = .lightGray.withAlphaComponent(0.3)
btnDropdown.configuration?.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = UIFont.systemFont(ofSize: 14, weight: .medium)
return outgoing
}
view.addSubview(btnDropdown)
NSLayoutConstraint.activate([
btnDropdown.heightAnchor.constraint(equalToConstant: 44),
btnDropdown.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20),
btnDropdown.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20),
btnDropdown.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30)
])
view.addSubview(myImageView)
NSLayoutConstraint.activate([
myImageView.trailingAnchor.constraint(equalTo: btnDropdown.trailingAnchor, constant: -8),
myImageView.topAnchor.constraint(equalTo: btnDropdown.topAnchor, constant: 8),
myImageView.bottomAnchor.constraint(equalTo: btnDropdown.bottomAnchor, constant: -8),
myImageView.widthAnchor.constraint(equalToConstant: 28) // 28 is the result of difference from 44 (the height of button) and the sum of padding top padding bottom (8 8): 44 - 16 = 28
])
這是結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/518441.html
上一篇:如何更改構建專案的二進制路徑?
