2016-08-02 6 views
1

I just create a new application project   1. 엑스 코드 버전 7.3 (7D175)
  2.swift 버전 2.2
  3.Mac OS 버전 OS X EI 탄 10.11.4
내가 맥 OS와 위한 데스크톱 응용 프로그램을 만들고 싶어 창 수준을 설정했지만 작동하지 않습니다.창을 항상 신속하게 유지하는 방법?

+1

설정 레벨 이전에 창 (! = nil)이 존재합니까? – larva

+0

윈도우 속성은 아마도 nil 일 것입니다. 구문과 관련이 없습니다. –

+0

http://stackoverflow.com/a/35714647/2303865 –

답변

1

당신은! 창을 확인 필요 = 전무 모든 사용자 지정 코드

class ViewController: NSViewController { 
    var addedObserver = false 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     if let window = self.view.window { 
      // custom window here 
      window.level = Int(CGWindowLevelForKey(.FloatingWindowLevelKey)) 
     } else { 
      addedObserver = true 
      self.addObserver(self, forKeyPath: "view.window", options: [.New, .Initial], context: nil) 
     } 
    } 

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { 
     if let window = self.view.window { 
      // custom window here 
      window.level = Int(CGWindowLevelForKey(.FloatingWindowLevelKey)) 
     } 
    } 

    deinit { 
     if addedObserver { 
      self.removeObserver(self, forKeyPath: "view.window") 
     } 
    } 
} 
+0

감사합니다. 내 프로젝트에 코드를 추가했지만 상단에 창을 유지할 수 없습니다. –

+0

@SteveJiang 코드가 // 여기에있는 사용자 정의 창에서 코드를 호출 했습니까? 나는 새로운 프로젝트를 만들고이 코드를 시도했다. 그것은 작동하고 창문을 글꼴 상단으로 가져 오지만, NSWindow 수준이 무엇인지 잘 모르겠습니다. – larva

+1

NSApplication.sharedApplication(). windows.first? .level =' –

0

전에 아직 그것을 테스트, 그러나 이것은 스위프트 4 유충의 대답이며 작동하는 것 같군.

var observingFloat = false 

override func viewDidLoad() { 
     super.viewDidLoad() 
     self.view.wantsLayer = true 

     if self.view.window != nil { 
      NSApplication.shared.windows.first?.level = NSWindow.Level(rawValue: 1) 
     } else { 
      observingFloat = true 
      self.addObserver(self, forKeyPath: "view.window", options: [.new, .initial], context: nil) 
     } 
    } 

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 

     if self.view.window != nil { 
      NSApplication.shared.windows.first?.level = NSWindow.Level(rawValue: 1) 
     } 
    } 
관련 문제