美文网首页
Mac OS 中 NSAlert的使用

Mac OS 中 NSAlert的使用

作者: 齐小益 | 来源:发表于2017-09-14 20:25 被阅读160次

    最近需要做出一个效果,带图片的弹窗。

    最开始想到的就是NSAlert

    一开始想改变NSAlert的大小去插入图片,确实成功了

    NSAlert *alert = [NSAlert alertWithMessageText:@"test" defaultButton:nil alternateButton:nil otherButton:nil informativeTextWithFormat:@""];
        
        
        NSWindow *container = alert.window;
        
        NSRect rect = container.frame;
        
        rect.size.height += 100;
        
        [container setFrame:rect display:YES];
        
        NSImageView *imageView = [[NSImageView alloc] init];
        
        
        
        imageView.image = [NSImage imageNamed:@"image"];
        
        imageView.frame = NSMakeRect(110, 55, imageWidth, imageHeight);
        
        [container.contentView addSubview:imageView];
    

    但是,以后再创建的NSAlert的大小全部变成了第一次设置时的大小,并且无法改变

    当然,如果你只是使用一次NSAlert的话,这样也没有什么问题。但是如果想后续使用原始大小的NSAlert,这种方法就不行了。

    后来偶然发现了NSAlert的一个属性:accessoryView,可以把imageView赋值给它

    alert.accessoryView = imageView;
    

    这样也是成功了,但是你无法随意改变位置,也就是说你只能设置宽度和高度,系统会把我们的View放在一个固定的位置。
    但是好处是后续再弹出的NSAlert大小不会受之前的影响,还是原始大小。

    两个方法,根据实际场景做出取舍。

    相关文章

      网友评论

          本文标题:Mac OS 中 NSAlert的使用

          本文链接:https://www.haomeiwen.com/subject/dvfjsxtx.html