当 contextMenu 出现时约束重新激活

问题描述

我正在使用 uicontextmenuconfiguration 对集合视图单元格进行操作。一切都按预期工作,但是如果我的单元格从笔尖(取消)激活了约束,它会在长按时刷新。

为了演示这个问题,我创建了一个新项目,在故事板中有一个 public function initialize() { parent::initialize(); // ... $this->loadComponent('Authentication.Authentication'); $this->Authentication->getEventManager()->on( 'Authentication.afterIdentify',function ( \Cake\Event\EventInterface $event,\Authentication\Authenticator\AuthenticatorInterface $provider,\Authentication\IdentityInterface $identity,\Authentication\AuthenticationServiceInterface $service ) { // ... $identity['foo'] = 'bar'; $this->Authentication->setIdentity($identity); } ); } 。集合视图中的自定义单元格有一个带有两个约束的标签一个约束将其固定到单元格的底部初始激活),另一个将其对齐在中心(初始禁用).

这是我的代码

collectionView

Initial state

Context menu presentation

After context menu dismissal

解决方法

问题似乎与以下直接相关:

  • 在 Storyboard 中创建的两个约束会相互冲突
  • 将一个约束设置为未安装
  • 取消激活已安装约束并激活未安装约束

当使用 UIContextMenuConfiguration 初始化 previewProvider: nil 时 - 它告诉 UIKit 自动生成一个预览视图 - 约束的“安装”状态被重置。

一种解决方法:

不要将中心约束标记为“未安装”,而是为其赋予 998 的优先级,并为底部约束赋予 999 的优先级(这将防止 IB 抱怨)。

然后您可以将 configure() 函数保持为:

func configure() {
    bottomConstraint.isActive = false
    centerConstraint.isActive = true
}

并且标签的 centerY 约束将在单元格和上下文菜单的预览中保持活动状态。

但是,如果您想要预览视图看起来与单元格不同,那么您最好的选择(并且可能是更好的开始方法)是使用自定义 { {1}}。