83

Dockアイコンを非表示にして。を表示することを優先したいNSStatusItem。StatusItemを作成できますが、Dockからアイコンを削除する方法がわかりません。:-/

何か案は?

4

7 に答える 7

86

アクティベーション ポリシーと呼ばれるものを使用できます。

Objective-C

// The application is an ordinary app that appears in the Dock and may
// have a user interface.
[NSApp setActivationPolicy: NSApplicationActivationPolicyRegular];

// The application does not appear in the Dock and does not have a menu
// bar, but it may be activated programmatically or by clicking on one
// of its windows.
[NSApp setActivationPolicy: NSApplicationActivationPolicyAccessory];

// The application does not appear in the Dock and may not create
// windows or be activated.
[NSApp setActivationPolicy: NSApplicationActivationPolicyProhibited];

スイフト4

// The application is an ordinary app that appears in the Dock and may
// have a user interface.
NSApp.setActivationPolicy(.regular)

// The application does not appear in the Dock and does not have a menu
// bar, but it may be activated programmatically or by clicking on one
// of its windows.
NSApp.setActivationPolicy(.accessory)

// The application does not appear in the Dock and may not create
// windows or be activated.
NSApp.setActivationPolicy(.prohibited)

これにより、ドック アイコンが非表示になります。

こちらもご覧ください

于 2012-02-10T00:01:56.437 に答える
50

アプリケーション バンドルを変更しないという Apple のガイドラインに従い、Mac App Store アプリ/(Lion アプリ?) の署名が info.plist の変更によって壊れないようにするために、デフォルトで LSUIElement を 1 に設定します。アプリケーションの起動は次のことを行います:

ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToForegroundApplication);

ドックアイコンを表示するか、ユーザーがアイコンを望まない場合はこれをバイパスします。

副作用が 1 つだけあります。アプリケーションのメニューは、フォーカスを失って回復するまで表示されません。

出典: Dock アイコンのオンとオフを切り替えるチェックボックスを作成する

個人的には、Info.plistオプションを設定せず、ユーザー設定に基づいてTransformProcessType(&psn, kProcessTransformToForegroundApplication)orを使用することを好みます。TransformProcessType(&psn, kProcessTransformToUIElementApplication)

于 2011-01-14T00:11:14.950 に答える
29

Xcode では「アプリケーションはエージェント (UIElement) です」と表示され、ブール値です。

Info.plist で空のスペースをコントロール クリックし、メニューから [行の追加] を選択します。「アプリケーションはエージェント (UIElement)」と入力します。YES に設定します。

オプションにするために、次の行をコードに追加しました(Valexaに感謝します!)

 // hide/display dock icon
if (![[NSUserDefaults  standardUserDefaults] boolForKey:@"hideDockIcon"]) {
    //hide icon on Dock
    ProcessSerialNumber psn = { 0, kCurrentProcess };
    TransformProcessType(&psn, kProcessTransformToForegroundApplication);
} 
于 2011-05-24T12:28:57.053 に答える
12

Swift の更新: (上記の両方の方法を使用すると、同じ結果が得られます)

public class func toggleDockIcon_Way1(showIcon state: Bool) -> Bool {
    // Get transform state.
    var transformState: ProcessApplicationTransformState
    if state {
        transformState = ProcessApplicationTransformState(kProcessTransformToForegroundApplication)
    }
    else {
        transformState = ProcessApplicationTransformState(kProcessTransformToUIElementApplication)
    }

    // Show / hide dock icon.
    var psn = ProcessSerialNumber(highLongOfPSN: 0, lowLongOfPSN: UInt32(kCurrentProcess))
    let transformStatus: OSStatus = TransformProcessType(&psn, transformState)
    return transformStatus == 0
}

public class func toggleDockIcon_Way2(showIcon state: Bool) -> Bool {
    var result: Bool
    if state {
        result = NSApp.setActivationPolicy(NSApplicationActivationPolicy.Regular)
    }
    else {
        result = NSApp.setActivationPolicy(NSApplicationActivationPolicy.Accessory)
    }
    return result
}
于 2014-10-09T16:58:05.240 に答える