上下文菜单选择颜色和框架大小 Swift 5 iOS 13
Context Menu selection color and frame size Swift 5 iOS 13
我正在尝试通过新的 iOS 13 上下文菜单为我的应用程序实现一个简单的 report/block 功能。它似乎工作正常,但是,我遇到了它的外观问题,我暂时无法解决。
- 当我长按
UICollectionViewCell
时,它会在黑暗模式下突出显示黑色,这看起来很糟糕,因为我没有全黑背景颜色。如何将其更改为清晰的颜色或我想要的颜色?
- 我有一个显示用户消息的气泡,我想使用气泡的上下文菜单。问题是,当上下文菜单默认预览我的单元格时,它会裁剪气泡的底部,它看起来只是整个消息的一部分。我尝试调整单元格的高度和其他一些参数——没有任何效果。
我该如何解决这些问题?请帮忙。
****我的密码是:****
@available(iOS 13.0, *)
override func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil){ action in
let cell = collectionView.cellForItem(at: IndexPath.init(row: indexPath.row, section: 0)) as? ChatCell
cell?.tintColor = .clear
self.view.backgroundColor = .clear
let messageText = cell?.textView.text
let userNameZdes = cell?.nameView.text
let report = UIAction(title: "Report", image: UIImage(systemName: "exclamationmark.bubble"), identifier: UIAction.Identifier(rawValue: "report")) {_ in
print("report clicked..")
if (cell?.textView.text.count)! > 2 {
print("the text of the message = \(messageText!) & the user is \(userNameZdes!)")
} else {
print("the user has attached a bad pic")
}
}
let block = UIAction(title: "Block user", image: UIImage(systemName: "person.crop.circle.badge.xmark"), identifier: UIAction.Identifier(rawValue: "block"), attributes: .destructive) {_ in
print("block clicked..")
}
return UIMenu(title: "Message Actions", image: nil, identifier: nil, children: [report, block])
}
return configuration
}
我今天遇到了同样的问题,我找到了一个对我有用的解决方案,所以我希望它也对你有用。
我在这个 post 中找到了解决方案:https://kylebashour.com/posts/context-menu-guide
如果向下滚动到部分:UITargetedPreview
,这里有一个 UITableView
的示例,但是您可以对 UICollectionView 应用相同的解决方案:)
有同样的问题。我通过覆盖 UICollectionViewCell isHighlighted 属性 来修复它。当 didSet -> background color 与 viewcontrollers background color 相同时,你就完成了。
假设您的 viewcontrollers/collectionview backgroundcolor = black,在自定义 collectionviewcell 中 class:
override var isHighlighted: Bool {
didSet {
backgroundColor = .black
}
}
我正在尝试通过新的 iOS 13 上下文菜单为我的应用程序实现一个简单的 report/block 功能。它似乎工作正常,但是,我遇到了它的外观问题,我暂时无法解决。
- 当我长按
UICollectionViewCell
时,它会在黑暗模式下突出显示黑色,这看起来很糟糕,因为我没有全黑背景颜色。如何将其更改为清晰的颜色或我想要的颜色? - 我有一个显示用户消息的气泡,我想使用气泡的上下文菜单。问题是,当上下文菜单默认预览我的单元格时,它会裁剪气泡的底部,它看起来只是整个消息的一部分。我尝试调整单元格的高度和其他一些参数——没有任何效果。
我该如何解决这些问题?请帮忙。
****我的密码是:****
@available(iOS 13.0, *)
override func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil){ action in
let cell = collectionView.cellForItem(at: IndexPath.init(row: indexPath.row, section: 0)) as? ChatCell
cell?.tintColor = .clear
self.view.backgroundColor = .clear
let messageText = cell?.textView.text
let userNameZdes = cell?.nameView.text
let report = UIAction(title: "Report", image: UIImage(systemName: "exclamationmark.bubble"), identifier: UIAction.Identifier(rawValue: "report")) {_ in
print("report clicked..")
if (cell?.textView.text.count)! > 2 {
print("the text of the message = \(messageText!) & the user is \(userNameZdes!)")
} else {
print("the user has attached a bad pic")
}
}
let block = UIAction(title: "Block user", image: UIImage(systemName: "person.crop.circle.badge.xmark"), identifier: UIAction.Identifier(rawValue: "block"), attributes: .destructive) {_ in
print("block clicked..")
}
return UIMenu(title: "Message Actions", image: nil, identifier: nil, children: [report, block])
}
return configuration
}
我今天遇到了同样的问题,我找到了一个对我有用的解决方案,所以我希望它也对你有用。
我在这个 post 中找到了解决方案:https://kylebashour.com/posts/context-menu-guide
如果向下滚动到部分:UITargetedPreview
,这里有一个 UITableView
的示例,但是您可以对 UICollectionView 应用相同的解决方案:)
有同样的问题。我通过覆盖 UICollectionViewCell isHighlighted 属性 来修复它。当 didSet -> background color 与 viewcontrollers background color 相同时,你就完成了。
假设您的 viewcontrollers/collectionview backgroundcolor = black,在自定义 collectionviewcell 中 class:
override var isHighlighted: Bool {
didSet {
backgroundColor = .black
}
}