MacOS-MAC 开发和iOS开发不同之处(纯代码)

一、坐标系不同

iOS 零点在左上角
mac 零点在左下角

二、ViewController 初始化不同

iOS alloc init 会自动创建空view
mac alloc init不会主动创建,需重写-(void)loadview方法,否则会报nib找不到的错误

- (void)loadView{
    //NSMakeRect(0, 0, 250, 150)
    NSRect frame = [[[NSApplication sharedApplication] mainWindow] frame];
    self.view = [[NSView alloc] initWithFrame:frame];
}

三、TableView,CollectionView 使用方式不同

iOS 两者可单独使用
MAC 需配合NSScrollView 使用

_tableContainerView = [[NSScrollView alloc] initWithFrame:CGRectMake(0, 0, 100, self.view.frame.size.height)];
[_tableContainerView setDocumentView:_tableview];
[self.view addSubview:_tableContainerView];

注意NSCollectionView 选中 item 的代理方法 didSelectItemsAtIndexPaths 默认不会被调用,需要开启可选中属性

[_collectionView setSelectable:true];

TableView在iOS下只有一列,在Mac下增加了一个NSTableColumn类,也就是一个table可以包含多列

    NSTableColumn * column = [[NSTableColumn alloc]initWithIdentifier:@"test"];
    column.title = @"分类";
    self.tableview = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, 300, 100)];
    [self.tableview addTableColumn:column];
    [self.tableview setDraggingDestinationFeedbackStyle:NSTableViewDraggingDestinationFeedbackStyleGap];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;

四、UILabel 在Mac下不存在

Mac 使用NSTextField代替,设置为不可以编辑,不可选中

NSTextField *vi = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
[vi setStringValue:@"xxxx"];
vi.editable = false;
vi.bezeled = NO;
vi.selectable = NO;

五、NSIndexPath 区别

iOS: row = indexPath.row ,column = indexPath.column
Mac:row = indexPath.item, column = indexPath.section

六、背景色问题

MAC有些对象 没有 backgroundColor 属性,可通layer 设置,
记得设置 wantsLayer = YES

    self.view.wantsLayer = YES;
    self.view.layer.backgroundColor = [[NSColor redColor] CGColor];

如果背景色依然没有变化,设置 drawsBackground = YES;

七、button 字体颜色

Mac NSButton 没有textcolor属性

NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitle]];
NSRange titleRange = NSMakeRange(0, [colorTitle length]);
[colorTitle addAttribute:NSForegroundColorAttributeName value:color range:titleRange];
[button setAttributedTitle:colorTitle];

八、NSWindow 关闭窗口,dock恢复

- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag{
    if(!flag)
    {
        [NSApp activateIgnoringOtherApps:NO];
        [self.window makeKeyAndOrderFront:self];
    }
    return YES;
}

正常情况下点击应用左上角关闭按钮,应用会退出,如果不希望退出,请做如下设置

[self.window setReleasedWhenClosed:NO];

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值