使用基于约束的布局自动调整NSTokenField的大小 [英] Auto-resizing NSTokenField with constraint-based layout
本文介绍了使用基于约束的布局自动调整NSTokenField的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用约束自动调整NSTokenField的高度大小(保持宽度不变)?
Is there any way to automatically resize the height of a NSTokenField (keeping the width constant) using constraints?
-sizeToFit
应该可以,但是不能。如果我设置了一个约束来保持宽度恒定并调用此方法,它将忽略该约束并仅调整宽度(当我想要的只是调整高度时)。
-sizeToFit
should work, but it doesn't. If I set a constraint to keep the width constant and call this method it ignores the constraints and resizes the width only (when what I want is to resize the height only).
推荐答案
token字段的 cellSizeForBounds
方法确实返回正确的大小,因此您可以像下面这样实现它(自定义子类,在Swift中):
The cellSizeForBounds
method of the token field does return the correct size, so you can implement it like this (custom subclass, in Swift):
class TagsTokenField: NSTokenField {
override func textDidChange(notification: NSNotification) {
super.textDidChange(notification)
self.invalidateIntrinsicContentSize()
}
override var intrinsicContentSize: NSSize {
let size = self.cell!.cellSizeForBounds(NSMakeRect(0, 0, self.bounds.size.width, 1000))
return NSMakeSize(CGFloat(FLT_MAX), size.height)
}
}
这篇关于使用基于约束的布局自动调整NSTokenField的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文