本文作为快速创建项目入门,细节不做阐述,请自行探索
Mac OS X 的坐标系不同于IOS
IOS 原点在屏幕左上角
Mac OS X 原点在左下角
Mac alloc init 一个viewcontroll 不会自动创建view
IOS 会自动创建view
所以Mac OS 的 ViewController
都需要实现loadView 方法
初始化view
创建项目
启动 Xcode
选择 创建新项目
顶部 选择macOS
Application 下 选择 cooca App
点击next
取消勾选Use Storyboards
点击next
既然是纯代码开发
那么移除目录中所有.xib文件
也就是把所有.xib结尾的都删掉
同时info.plist 移除对应的key
完成项目创建
编写代码
编辑main.m,否则会出现窗口不显示
#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
//窗口 关闭,缩小,放大等功能,根据需求自行组合
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
创建第一个ViewController
快捷键commad+n
编辑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
运行一下,看看效果吧
更多功能自行探索
你会成长很多