"insetGrouped" 样式的单元格预览圆角 (iOS 13)
Rounded corners for cell preview in the "insetGrouped" style (iOS 13)
当 UITableView 处于 insetGrouped
样式时,我发现了一个烦人的问题。当实现 contextMenuConfigurationForRowAtIndexPath
方法时,它允许使用上下文操作来查看和弹出单元格预览。一切正常,除了 iOS 预览 window 圆角时的功能。
但是,如果 table 在所有其他样式(例如,普通样式)中,边角会变圆。此外,普通模式下的动画更多 "smooth" 并且预览会缩小一点。
我还发现 iOS 在 insetGrouped
样式中将第一个单元格的顶角和最后一个单元格的底角圆化。
有没有人遇到过类似UITableView
的行为?
您可以通过实现方法为单元格设置圆角 'previewForHighlightingContextMenuWithConfiguration'
- (UITargetedPreview *)tableView:(UITableView *)tableView previewForHighlightingContextMenuWithConfiguration:(UIContextMenuConfiguration *)configuration {
NSIndexPath *index = (NSIndexPath *)configuration.identifier;
SharedTableViewCell *cell = [self.tableView cellForRowAtIndexPath:index];
UIPreviewParameters *parameters = [[UIPreviewParameters alloc] init];
parameters.backgroundColor = UIColor.clearColor;
parameters.visiblePath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:10.0];
UITargetedPreview *targetedPreview = [[UITargetedPreview alloc] initWithView:cell parameters:parameters];
return targetedPreview;
}
override func tableView(_ tableView: UITableView,
previewForDismissingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
return self.tableView(tableView, previewForHighlightingContextMenuWithConfiguration: configuration)
}
override func tableView(_ tableView: UITableView,
previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
guard let indexPath = configuration.identifier as? NSIndexPath else { return nil }
guard let cell = tableView.cellForRow(at: indexPath as IndexPath) as? YourCellNameTableViewCell else { return nil }
let parameters = UIPreviewParameters()
parameters.backgroundColor = .clear
parameters.visiblePath = UIBezierPath(roundedRect: cell.contentView.bounds, cornerRadius: cell.contentView.layer.cornerRadius)
let preview = UITargetedPreview(view: cell.contentView, parameters: parameters)
return preview
}
不要忘记在您的单元格中设置:
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
layer.cornerRadius = 10.0
clipsToBounds = true
}
同样的事情适用于 UICollectionViewCell,
override func collectionView(_ collectionView: UICollectionView,
previewForDismissingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
return self.collectionView(collectionView, previewForHighlightingContextMenuWithConfiguration: configuration)
}
override func collectionView(_ collectionView: UICollectionView,
previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
guard let indexPath = configuration.identifier as? NSIndexPath else { return nil }
guard let cell = collectionView.cellForItem(at: indexPath as IndexPath) as? YourCellNameCollectionViewCell else { return nil }
let parameters = UIPreviewParameters()
parameters.backgroundColor = .clear
parameters.visiblePath = UIBezierPath(roundedRect: cell.contentView.bounds, cornerRadius: cell.contentView.layer.cornerRadius)
let preview = UITargetedPreview(view: cell.contentView, parameters: parameters)
return preview
}
Swift版本[Tài Mẫn Tiến答案]
确保正确返回 IndexPath。
return UIContextMenuConfiguration(identifier: indexPath as NSCopying, previewProvider: nil,actionProvider: { suggestedActions in })
Swift代码:
func tableView(_ tableView: UITableView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
guard let indexPath = configuration.identifier as? IndexPath else { return nil }
guard let cell = tableView.cellForRow(at: indexPath) as? YourCellName else {
return nil }
let parameters = UIPreviewParameters()
parameters.backgroundColor = UIColor.clear
parameters.visiblePath = UIBezierPath(roundedRect: cell.bounds , cornerRadius: 10.0)
var targetedPreview: UITargetedPreview? = nil
targetedPreview = UITargetedPreview(view: cell, parameters: parameters)
return targetedPreview
}
当 UITableView 处于 insetGrouped
样式时,我发现了一个烦人的问题。当实现 contextMenuConfigurationForRowAtIndexPath
方法时,它允许使用上下文操作来查看和弹出单元格预览。一切正常,除了 iOS 预览 window 圆角时的功能。
但是,如果 table 在所有其他样式(例如,普通样式)中,边角会变圆。此外,普通模式下的动画更多 "smooth" 并且预览会缩小一点。
我还发现 iOS 在 insetGrouped
样式中将第一个单元格的顶角和最后一个单元格的底角圆化。
有没有人遇到过类似UITableView
的行为?
您可以通过实现方法为单元格设置圆角 'previewForHighlightingContextMenuWithConfiguration'
- (UITargetedPreview *)tableView:(UITableView *)tableView previewForHighlightingContextMenuWithConfiguration:(UIContextMenuConfiguration *)configuration {
NSIndexPath *index = (NSIndexPath *)configuration.identifier;
SharedTableViewCell *cell = [self.tableView cellForRowAtIndexPath:index];
UIPreviewParameters *parameters = [[UIPreviewParameters alloc] init];
parameters.backgroundColor = UIColor.clearColor;
parameters.visiblePath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:10.0];
UITargetedPreview *targetedPreview = [[UITargetedPreview alloc] initWithView:cell parameters:parameters];
return targetedPreview;
}
override func tableView(_ tableView: UITableView,
previewForDismissingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
return self.tableView(tableView, previewForHighlightingContextMenuWithConfiguration: configuration)
}
override func tableView(_ tableView: UITableView,
previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
guard let indexPath = configuration.identifier as? NSIndexPath else { return nil }
guard let cell = tableView.cellForRow(at: indexPath as IndexPath) as? YourCellNameTableViewCell else { return nil }
let parameters = UIPreviewParameters()
parameters.backgroundColor = .clear
parameters.visiblePath = UIBezierPath(roundedRect: cell.contentView.bounds, cornerRadius: cell.contentView.layer.cornerRadius)
let preview = UITargetedPreview(view: cell.contentView, parameters: parameters)
return preview
}
不要忘记在您的单元格中设置:
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
layer.cornerRadius = 10.0
clipsToBounds = true
}
同样的事情适用于 UICollectionViewCell,
override func collectionView(_ collectionView: UICollectionView,
previewForDismissingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
return self.collectionView(collectionView, previewForHighlightingContextMenuWithConfiguration: configuration)
}
override func collectionView(_ collectionView: UICollectionView,
previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
guard let indexPath = configuration.identifier as? NSIndexPath else { return nil }
guard let cell = collectionView.cellForItem(at: indexPath as IndexPath) as? YourCellNameCollectionViewCell else { return nil }
let parameters = UIPreviewParameters()
parameters.backgroundColor = .clear
parameters.visiblePath = UIBezierPath(roundedRect: cell.contentView.bounds, cornerRadius: cell.contentView.layer.cornerRadius)
let preview = UITargetedPreview(view: cell.contentView, parameters: parameters)
return preview
}
Swift版本[Tài Mẫn Tiến答案]
确保正确返回 IndexPath。
return UIContextMenuConfiguration(identifier: indexPath as NSCopying, previewProvider: nil,actionProvider: { suggestedActions in })
Swift代码:
func tableView(_ tableView: UITableView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? { guard let indexPath = configuration.identifier as? IndexPath else { return nil } guard let cell = tableView.cellForRow(at: indexPath) as? YourCellName else { return nil } let parameters = UIPreviewParameters() parameters.backgroundColor = UIColor.clear parameters.visiblePath = UIBezierPath(roundedRect: cell.bounds , cornerRadius: 10.0) var targetedPreview: UITargetedPreview? = nil targetedPreview = UITargetedPreview(view: cell, parameters: parameters) return targetedPreview }