SwiftQQ 登陸介面
阿新 • • 發佈:2020-12-28
技術標籤:MacOS
1.新建一個專案,在storyboard中設定其大小為250x320,然後拖拽一個imageView填充滿整個view,並設定其填充屬性為Axes Independently。最後勾選上windows的fullSizeContentView屬性。使內容檢視佔據整個視窗大小。
2.設定UI的介面
左上角是一個ImageViewButton,中間是一個ImageView, 下面是textFiled和一個textFiled的組合,這裡textFied邊框為空,背景色用取色器取成和周圍一樣。然後一個登陸按鈕,一個更多按鈕都用imageViewButton來設定。新增自己覺得合適的約束即可。
3.這時視窗是不能移動且沒有圓角的。新建一個LoginWindowController類繼承自windowcontroller我們加上下面的程式碼
class LoginWindowController: NSWindowController { override func windowDidLoad() { super.windowDidLoad() //設定拖動視窗背景可移動 window?.isMovableByWindowBackground = true //設定背景色透明 window?.backgroundColor = NSColor.clear } }
並在viewController中設定背景透明
override func viewDidLoad() {
super.viewDidLoad()
view.layer?.backgroundColor = NSColor.clear.cgColor
// Do any additional setup after loading the view.
}
一般情況下上面的程式碼可以去除視窗的邊角並可以移動了,但是我們用imageView覆蓋著整個檢視,導致滑鼠被子控制元件imageView捕獲,所以是移動不了的。新建一個類backgroundImageView,重寫mouseDownCanMoveWindow方法即可
override var mouseDownCanMoveWindow: Bool{
return true
}
以為之前去掉了titlebar所以現在是接受不了鍵盤輸入事件的。新建一個NSWindow,重寫canbecomekey方法
/* 重寫這個方法: 讓視窗預設支援為key Window
只有key window 才能響應鍵盤焦點事件,實現輸入操作 */
override var canBecomeKey: Bool{
return true
}
返回storyboard設定window的customClass為loginwindow即可
設定textFiled的fousRing