本文作為快速創(chuàng)建項(xiàng)目入門,細(xì)節(jié)不做闡述,請(qǐng)自行探索
Mac OS X 的坐標(biāo)系不同于IOS
IOS 原點(diǎn)在屏幕左上角
Mac OS X 原點(diǎn)在左下角
Mac alloc init 一個(gè)viewcontroll 不會(huì)自動(dòng)創(chuàng)建view
IOS 會(huì)自動(dòng)創(chuàng)建view
所以Mac OS 的 ViewController
都需要實(shí)現(xiàn)loadView 方法
初始化view
創(chuàng)建項(xiàng)目
啟動(dòng) Xcode
選擇 創(chuàng)建新項(xiàng)目
頂部 選擇macOS
Application 下 選擇 cooca App

屏幕快照 2018-06-19 下午12.54.41.png
點(diǎn)擊next
取消勾選Use Storyboards

屏幕快照 2018-06-19 下午12.55.13.png
點(diǎn)擊next
既然是純代碼開(kāi)發(fā)
那么移除目錄中所有.xib文件
也就是把所有.xib結(jié)尾的都刪掉
同時(shí)info.plist 移除對(duì)應(yīng)的key

屏幕快照 2018-06-19 下午1.43.07.png
完成項(xiàng)目創(chuàng)建
編寫代碼
編輯main.m,否則會(huì)出現(xiàn)窗口不顯示
#import <Cocoa/Cocoa.h>
#import "AppDelegate.h"
int main(int argc, const char * argv[]) {
AppDelegate *delegate = [[AppDelegate alloc] init];
[NSApplication sharedApplication].delegate = delegate;
return NSApplicationMain(argc, argv);
}
編輯appDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@property (nonatomic, strong) NSWindow *window;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
//窗口 關(guān)閉,縮小,放大等功能,根據(jù)需求自行組合
NSUInteger style = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
float w = [[NSScreen mainScreen] frame].size.width/2;
float h = [[NSScreen mainScreen] frame].size.height/2;
self.window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, w, h) styleMask:style backing:NSBackingStoreBuffered defer:YES];
self.window.title = @"hello";
[self.window makeKeyAndOrderFront:self];
[self.window center];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
@end
創(chuàng)建第一個(gè)ViewController
快捷鍵commad+n

屏幕快照 2018-06-19 下午1.06.55.png

屏幕快照 2018-06-19 下午1.07.28.png
編輯HomeViewController
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
- (void)loadView{
NSRect frame = [[[NSApplication sharedApplication] mainWindow] frame];
self.view = [[NSView alloc] initWithFrame:frame];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
//do like ios
NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
[button setTitle:@"button"];
[self.view addSubview:button];
}
@end
再次編輯appdelete.m
#import "AppDelegate.h"
#import "HomeViewController.h"
@interface AppDelegate ()
@property (nonatomic, strong) NSWindow *window;
@property (nonatomic, strong) HomeViewController *homeVC;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
//
NSUInteger style = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
float w = [[NSScreen mainScreen] frame].size.width/2;
float h = [[NSScreen mainScreen] frame].size.height/2;
self.window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, w, h) styleMask:style backing:NSBackingStoreBuffered defer:YES];
self.window.title = @"hello";
[self.window makeKeyAndOrderFront:nil];
[self.window center];
self.homeVC = [[HomeViewController alloc] init];
[self.window setContentViewController:self.homeVC];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
@end
運(yùn)行一下,看看效果吧
更多功能自行探索
你會(huì)成長(zhǎng)很多