英文:
How to configure button via UIButton.Configuration with custom SFSymbol?
问题
我创建了一个按钮:
```swift
private let sortButton: UIButton = {
let buttonConfig = UIButton.Configuration.plain()
let button = UIButton(configuration: buttonConfig)
button.tintColor = R.color.controlSecondaryTypo()
button.showsMenuAsPrimaryAction = true
button.changesSelectionAsPrimaryAction = true
return button
}()
接下来我添加了菜单:
private func setupButton() {
let items = ["新建", "旧的"]
let actions: [UIAction] = items.map {
let action = UIAction(title: $0) { action in
print("\(action.title)")
}
return action
}
let menu = UIMenu(options: .singleSelection, children: actions)
sortButton.menu = menu
}
这是我的按钮显示enter image description here
它默认显示为 chevron.up.chevron.down。如何改成另一个 SFSymbol?例如:chevron.down
我想要能够配置按钮,根据所选的 UIMenu 元素更改标题,并使用自定义的 SFSymbol。
<details>
<summary>英文:</summary>
I create my button:
private let sortButton: UIButton = {
let buttonConfig = UIButton.Configuration.plain()
let button = UIButton(configuration: buttonConfig)
button.tintColor = R.color.controlSecondaryTypo()
button.showsMenuAsPrimaryAction = true
button.changesSelectionAsPrimaryAction = true
return button
}()
Next I add the menu:
private func setupButton() {
let items = ["New", "Old"]
let actions: [UIAction] = items.map {
let action = UIAction(title: $0) { action in
print("\(action.title)")
}
return action
}
let menu = UIMenu(options: .singleSelection, children: actions)
sortButton.menu = menu
}
Here is the display of my button[enter image description here](https://i.stack.imgur.com/CJOPy.png)
It is always displayed by default with chevron.up.chevron.down. How can I change to another SFSymbol? Example: chevron.down
I want to be able to configure button that will change the title depending on the selected UIMenu element with a custom SFSymbol
</details>
# 答案1
**得分**: 0
因为使用了`UIButton.Configuration.plain()`,在当前实现中我没有看到通过配置初始化按钮的任何理由,你可以使用`Button()`,然后添加菜单。
但如果你需要配置的话,可以使用`.borderless()`,那个配置可能是你在寻找的。
或者通过扩展创建自己的配置,并对其进行设置。
顺便说一句,如果你的需求需要`.plain()`,你可以做一些巧妙的事情😅😬,比如:
```swift
sortButton.subviews.first(where: { $0 is UIImageView })?.isHidden = true
但我认为简单的Button()
或者.borderless()
就足够了。
英文:
It is happening because of UIButton.Configuration.plain()
, in current implementation I do not see any reason to init button through config, you can Button() and after add you menu.
But if you needed config then use .borderless()
, that config might be the one you looking for.
Or make your own config by extension and play with settings of it.
Btw, if .plain()
is required for your desires you can make something hacky😅😬, like:
sortButton.subviews.first(where: { $0 is UIImageView })?.isHidden = true
but I think simple Button() or .borderless()
would be enough
答案2
得分: 0
当你使用带有菜单的UIButton并启用showsMenuAsPrimaryAction
和changesSelectionAsPrimaryAction
这两个属性时,按钮会自动显示chevron.up.chevron.down
指示器,以表示这是一个弹出式按钮。
UIButton.Configuration
有一个名为indicator
的属性,默认情况下在将按钮设置为弹出式按钮时为.popup
。
由于这是一个标准做法,你应该考虑保留图标不变,因为用户会认识它。
但如果你确实想要更改图标,你需要应用自己的按钮图像并移除指示器。
将你的按钮配置代码更改为:
var buttonConfig = UIButton.Configuration.plain()
buttonConfig.indicator = .none // 隐藏标准弹出图标
buttonConfig.image = UIImage(systemName: "chevron.down", withConfiguration: UIImage.SymbolConfiguration(scale: .small)) // 使用新图标
buttonConfig.imagePlacement = .trailing // 将图像放在标题之后
英文:
When you setup a UIButton with a menu and you enable the two properties showsMenuAsPrimaryAction
and changesSelectionAsPrimaryAction
, then the button displays the chevron.up.chevron.down
indicator automatically to indicate that this is a popup button.
UIButton.Configuration
has an indicator
property that defaults to .popup
when you setup the button as a popup button.
Since this is a standard, you should consider leaving the icon as-is since users will recognize what it is.
But if you do want to change the icon, you will need to apply your own button image and remove the indicator.
Change your button configuration code to:
var buttonConfig = UIButton.Configuration.plain()
buttonConfig.indicator = .none // Hide the standard popup icon
buttonConfig.image = UIImage(systemName: "chevron.down", withConfiguration: UIImage.SymbolConfiguration(scale: .small)) // Use the new icon
buttonConfig.imagePlacement = .trailing // Put the image after the title
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论