![](/img/trans.png)
[英]How to change selectItem in NSPopUpButtonCell NSTableView Swift 4.2?
[英]How to draw NSPopupButtonCell highlighted image
我在NSToolbar
中有 NSPopupButton ,它的borderder = false
突出顯示時有一些顏色混合。 如果我使用bordered = true
,則圖像是用漂亮的深色疊加繪制的。
我正在嘗試以與bordered bordered=true
相同的方式繪制突出顯示的state
PS:帶有bordered = false
的NSButtonCell 開箱即用。
我可以通過使用bordered bordered = true
並覆蓋drawBezel
來實現該行為,並且在那里什么都不做,但我想知道
我嘗試過的事情:
-
class ToolbarPopUpButtonCell : NSPopUpButtonCell {
override var isHighlighted: Bool {
get { return true }
set { super.isHighlighted = newValue }
}
override func drawImage(withFrame cellFrame: NSRect, in controlView: NSView) {
super.drawImage(withFrame: cellFrame, in: controlView)
}
//used in case bordered = true so we do nothing
override func drawBezel(withFrame frame: NSRect, in controlView: NSView) {
}
//doesn't work
override var interiorBackgroundStyle: NSView.BackgroundStyle
{
return .raised
}
}
class ToolbarPopUpButton: NSPopUpButton {
override func awakeFromNib() {
cell?.setCellAttribute(.cellLightsByBackground, to: 1)
}
override var intrinsicContentSize: NSSize {
return NSMakeSize(32 + 5, 32)
}
}
注意右邊的圖像,它適用於bordered = false
( NSButtonCell
)
我發現的唯一技巧是繪制一個替代單元格。 仍在尋求更好的解決方案。
- (void)drawImageWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSImage *image = [self image];
NSButtonCell *swapCell = [[NSButtonCell alloc] initImageCell:image];
[swapCell setHighlighted:[self isHighlighted]];
[swapCell drawImage:image withFrame:cellFrame inView:controlView];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.