2017-11-14 5 views
0

私はNSPopoverを使用するメニューバーアプリケーションで作業しています。私はポップオーバーを提示するために次のコードを使用しています。NSPopoverとNSStatusItemでスペーシングを追加する

[self.mainPopover showRelativeToRect:[testView bounds] ofView:testView preferredEdge:NSMinYEdge]; 

問題は以下のようにステータスバーに近すぎますか?私は、ドキュメントが

ポップオーバーを配置すべきpositioningView相対以内矩形を述べたように、それは、当然のように任意の効果を有しておらずRECT変更しても

enter image description here

。通常は、positioningViewの境界に設定されます。空の矩形でも構いません。これは、デフォルトでは、positioningViewの境界になります。

以下は、dropboxアプリのスクリーンショットです。私はdropboxのようなアプリにいくつかのスペースを追加する方法を知りました。

enter image description here

答えて

1

これを実現するために、私は、パディングビューを追加し、そのコンテナビューにNSStatusItemビューを設定付けます。使用されるソリューションのコードは、それを実装しようとする人にとっては以下の通りです。

_paddingView = [NSView new]; 
[_containerView addSubview:_paddingView]; 
[_containerView addSubview:_dragView]; 

[_dragView  setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[_paddingView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[_containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_dragView(22)][_paddingView(5)]|"                             options:0 
                     metrics:nil views:views]]; 
[_containerView addConstraint:[NSLayoutConstraint constraintWithItem:_dragView 
                  attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual 
                   toItem:_paddingView attribute:NSLayoutAttributeLeft 
                  multiplier:1. constant:0]]; 
[_containerView addConstraint:[NSLayoutConstraint constraintWithItem:_dragView 
                  attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual 
                   toItem:_paddingView attribute:NSLayoutAttributeRight 
                  multiplier:1. constant:0]]; 

self.mainPopover = [[NSPopover alloc] init]; 
self.mainPopover.delegate = self; 
self.mainPopover.backgroundColor = [NSColor greenColor]; 
[self.mainPopover setAnimates:NO]; 
[self.mainPopover setBehavior:NSPopoverBehaviorTransient]; 
[self.mainPopover setContentViewController:viewController]; 

[_containerView layoutSubtreeIfNeeded]; 

[_statusItem setView:_containerView]; 
+1

これは私がSwiftで解決した方法ですhttps://stackoverflow.com/questions/48594212/how-to-open-a-nspopover-at-a-distance-from-the-system-バー/ 48604455#48604455 – Pier

0

あなたはデビューとポップオーバーの間にある程度のマージンを追加するために、テストビューの境界をはめ込むことができます:

NSPopover *mainPopover = [self mainPopover]; 
NSRect bounds = CGRectInset([testView bounds], -50.0, -50.0); 
[mainPopover showRelativeToRect:bounds ofView:testView preferredEdge:NSMinYEdge]; 
+0

これは、ドキュメントの状態 'postioningrect'として動作しませんがそれは私が窓の内側に配置NSButtonのために働くpositioningView –

+0

内の長方形であるが、私はそれが外に行くことはできませんNSMenuItemButtonの含まれたウィンドウを推測します境界。 –

+0

これを実現するには、実際のビューの下に非表示のビューをレイアウトする必要がありました。 –

関連する問題