gpt4 book ai didi

ios - 在 UIButton 中使用 UIBarButtonItem 图标

转载 作者:IT王子 更新时间:2023-10-29 07:45:53 24 4
gpt4 key购买 nike

UIBarButtonItem 有多个可用图标。是否可以使用将其标识符设置为“垃圾桶”后出现的图标:

trash icon

UIButton?没有像设置identifierstyle 那样直接的方法来做到这一点。

最佳答案

iOS 13 支持 SF Symbols现在
UIImage(systemName: "trash")


对于 swift 4.2 (在主线程上调用)

extension UIBarButtonItem.SystemItem {
func image() -> UIImage? {
let tempItem = UIBarButtonItem(barButtonSystemItem: self,
target: nil,
action: nil)

// add to toolbar and render it
let bar = UIToolbar()
bar.setItems([tempItem],
animated: false)
bar.snapshotView(afterScreenUpdates: true)

// got image from real uibutton
let itemView = tempItem.value(forKey: "view") as! UIView
for view in itemView.subviews {
if let button = view as? UIButton,
let image = button.imageView?.image {
return image.withRenderingMode(.alwaysTemplate)
}
}

return nil
}
}

UIBarButtonSystemItem.play.image()

对于 Objective-C:

+ (UIImage *)imageFromSystemBarButton:(UIBarButtonSystemItem)systemItem {
UIBarButtonItem* tempItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:nil action:nil];

// Add to toolbar and render it
UIToolbar *bar = [[UIToolbar alloc] init];
[bar setItems:@[tempItem] animated:NO];
[bar snapshotViewAfterScreenUpdates:YES];

// Get image from real UIButton
UIView *itemView = [(id)tempItem view];
for (UIView* view in itemView.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
return [(UIButton*)view imageForState:UIControlStateNormal];
}
}

return nil;
}

关于ios - 在 UIButton 中使用 UIBarButtonItem 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21187885/

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