macOS 開發(fā)問題匯總

目錄 11+

11. macOS 調(diào)用framework中類別的方法 編譯成功但運行報錯
12. macOS語言本地化/國際化同iOS一致块促,參見:iOS語言本地化/國際化一些技巧
13. macOS 打包問題記錄
14. macOS 完全清理App
15. xib嵌套
16. macOS 修改App名稱
17. macOS App Main Menu 工具欄

目錄1-10

1. 瀏覽器打開URL

  [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.baidu.com"]];

2. NSTextField超鏈接

//policy
NSFont *font = [NSFont systemFontOfSize:12];
attStr = [[NSMutableAttributedString alloc] initWithString:@"登錄即同意《用戶協(xié)議》和《隱私政策》"];
[attStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, 18)];
[attStr addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithRed:175.0f/255.0f green:174.0f/255.0f blue:174.0f/255.0f alpha:1.0f] range:NSMakeRange(0, 18)];
NSRange range = [[attStr string] rangeOfString:@"《用戶協(xié)議》"];

//policy-1
[attStr addAttribute:NSFontAttributeName value:font range:range];
[attStr addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithRed:5.0f/255.0f green:164.0f/255.0f blue:255.0f/255.0f alpha:1.0f] range:range];;
[attStr addAttribute:NSLinkAttributeName value:kCFPrivacyTerms_URL range:range];

//policy-2
range = [[attStr string] rangeOfString:@"《隱私政策》"];
[attStr addAttribute:NSFontAttributeName value:font range:range];
[attStr addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithRed:5.0f/255.0f green:164.0f/255.0f blue:255.0f/255.0f alpha:1.0f] range:range];
[attStr addAttribute:NSLinkAttributeName value:kCFPrivacyPolicy_URL range:range];

self.tfPolicy.enabled = YES;
self.tfPolicy.editable = NO;//必須禁止輸入琳彩,否則點擊將彈出輸入鍵盤
self.tfPolicy.selectable = YES;// 超連接
self.tfPolicy.allowsEditingTextAttributes = YES;// 超連接
self.tfPolicy.attributedStringValue = attStr;

3. NSTextField光標顏色 NSTextFieldCell和NSSecureTextFieldCell垂直居中 附加視圖

- (void)drawRect:(NSRect)dirtyRect {
    
    [super drawRect:dirtyRect];
    
    // Drawing code here.
    /*無效*/
//    NSView *view = [[NSView alloc] initWithFrame:CGRectMake(0, 0, NSWidth(dirtyRect), NSHeight(dirtyRect))];
//    view.layer.backgroundColor = [NSColor blueColor].CGColor;
//    [self addSubview:view];
}

-(BOOL) becomeFirstResponder {
    
    BOOL res = [super becomeFirstResponder];
    if ( res && self.hadSetInsertionPointColor == NO ) {
        
        NSTextView* textField = (NSTextView*)[self currentEditor];
        if ( [textField respondsToSelector: @selector(setInsertionPointColor:)] ) {
            
            self.hadSetInsertionPointColor = YES;
            [textField setInsertionPointColor:self.insertionPointColor?:self.textColor];
        }
    }
    return res;
}
@end

/**密碼輸入框NSSecureTextField內(nèi)容垂直居中問題
 NSSecureTextField-NSSecureTextFieldCell:不垂直居中
 NSSecureTextField-LTSecureTextFieldCell:不垂直居中
 NSTextField-LTSecureTextFieldCell:垂直居中
 
 分類方式:成為第一響應者后洒试,才有效;失去第一響應者后無效
 */
@interface LTSecureTextFieldCell : NSSecureTextFieldCell

/**邊界纸厉,可以自定義控制視圖等*/
@property (nonatomic, assign) NSEdgeInsets edgeInsets;

@end

@implementation LTTextFieldCell

#pragma mark - 1無效
//- (NSRect)titleRectForBounds:(NSRect)rect{
//
//    NSRect titleRect = [super titleRectForBounds:rect];
//    CGFloat minimHeight = self.cellSize.height;
//    titleRect.origin.y += (titleRect.size.height - minimHeight)/2;
//    titleRect.size.height = minimHeight;
//    return titleRect;
//}
//
//-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{
//  [super drawWithFrame:cellFrame inView:controlView];
//}
//
//-(void)selectWithFrame:(NSRect)rect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)delegate start:(NSInteger)selStart length:(NSInteger)selLength{
//
//  CGRect titleRect =  [self titleRectForBounds:rect];
//    titleRect.origin.y += 10;//self.oringeY;
//  [super selectWithFrame:titleRect inView:controlView editor:textObj delegate:delegate    start:selStart length:selLength];
// NSLog(@"titleRect = %@",NSStringFromRect(titleRect));
//
//}


#pragma mark - 2
- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame {
    
    // super would normally draw text at the top of the cell
    NSRect rect = CGRectMake(self.edgeInsets.left, self.edgeInsets.top, NSWidth(frame) - self.edgeInsets.left - self.edgeInsets.right, NSHeight(frame) - self.edgeInsets.top - self.edgeInsets.bottom);
    CGFloat fontSize = self.font.boundingRectForFont.size.height;
    NSInteger offset = floor((NSHeight(rect) - ceilf(fontSize))/2) - 2;
    NSRect centeredRect = NSInsetRect(rect, 0, offset);
    return centeredRect;
}

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event {

     [super editWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] inView:controlView editor:editor delegate:delegate event:event];
}

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)editor delegate:(id)delegate start:(NSInteger)start length:(NSInteger)length {

    [super selectWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] inView:controlView editor:editor delegate:delegate start:start length:length];
}

 - (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view {

     [super drawInteriorWithFrame:[self adjustedFrameToVerticallyCenterText:frame] inView:view];
}


#pragma mark - 3
/**實際內(nèi)容區(qū)域:修改了cell的frame,導致在設置背景色時田盈,部分區(qū)域無效*/
//- (NSRect)drawingRectForBounds:(NSRect)rect {
//    
//    NSRect rect1 = [super drawingRectForBounds:rect];
//    NSSize size = [self cellSizeForBounds:rect];
//    CGFloat h = rect1.size.height - size.height;
//    if ( h > 0 ) {
//        rect1.size.height = size.height;
//        rect1.origin.y += h * 0.5;
//    }
//    
//    return rect1;
//}

@end

4. 設置NSTextView失去光標

/**設置NSTextView失去光標*/
[self.tvEmail.window makeFirstResponder:nil];
[self.tvSuggest.window makeFirstResponder:nil];

5. 遮罩層MaskView

定制事件傳遞

/**遮罩視圖
 不調(diào)用super方法救赐,阻止了事件的傳遞;
 當前:攔截了鼠標的右鍵切油,左鍵蝙斜,又不會影響上層的視圖點擊*/
@interface LTMaskView : NSView

/**
 / **在superview中根據(jù)子視圖的identifier匹配LTMaskView,
 設置LTMaskView的部分事件繼續(xù)傳遞澎胡,不被攔截孕荠;并且在superview中也可以設置superview自身事件是否繼續(xù)傳遞娩鹉、攔截;
 * /
 [self.view.subviews enumerateObjectsUsingBlock:^(__kindof NSView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
     
     if ( [@"LTMaskView" isEqualToString:obj.identifier] ) {
         LTMaskView *maskView = (LTMaskView *)obj;
         maskView.allowedEventMask = NSEventMaskLeftMouseDown;
     }
 }];
 
 / **與設置里的自定義鼠標主鍵無關(guān)
 問題:present子視圖后稚伍,屏蔽了子視圖的動作弯予,只有superview的mouseDown事件了
 * /
 [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskLeftMouseDown handler:^NSEvent * _Nullable(NSEvent * _Nonnull aEvent) {

     [self mouseDown:aEvent];
     return nil;/ **事件被攔截,不會下傳* /
     return aEvent;/ **事件繼續(xù)傳遞* /
 }];
 */
@property (nonatomic, assign) NSEventMask allowedEventMask;

@end

#import "LTMaskView.h"

@implementation LTMaskView

- (instancetype)init {
    
    if ( self = [super init] ) {
        /**tag是只讀屬性个曙,但可以在xib中設置
         self.tag = 10001;
         */
        self.identifier = NSStringFromClass([self class]);
    
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.identifier = NSStringFromClass([self class]);
    }
    return self;
}

- (instancetype)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.identifier = NSStringFromClass([self class]);
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
}

- (void)mouseDown:(NSEvent *)event {
    
    if ( self.allowedEventMask & NSEventMaskLeftMouseDown ) {
        [super mouseDown:event];
    }
}

- (void)rightMouseDown:(NSEvent *)event {
    
    if ( self.allowedEventMask & NSEventMaskRightMouseDown ) {
        [super rightMouseDown:event];
    }
}

@end

6. 彈框NSAlert

- (void) testForWindow:(NSWindow *) window {
    
    NSAlert *alert = [[NSAlert alloc] init];
    alert.icon = nil;//[NSImage imageNamed:@"icon"];/**nil時锈嫩,默認APPicon*/
    alert.alertStyle = NSAlertStyleWarning;
    [alert addButtonWithTitle:@"確定"];
//    [alert addButtonWithTitle:@"取消"];/**可以注釋*/
//    alert.messageText = @"title";/**可以注釋*/
//    alert.informativeText = @"msg";/**可以注釋*/
    alert.showsSuppressionButton = NO;/**是否展示Don't ask again*/
    
    /**
     方式1
     使用方式1的時候需要注意的是window不能為nil,
     不要講NSAlert添加在HomeVC的ViewDidLoad中垦搬,此時Window還沒創(chuàng)建成功呼寸。
     */
    [alert beginSheetModalForWindow:window completionHandler:^(NSModalResponse returnCode) {
        if (returnCode == NSAlertFirstButtonReturn) {
            NSLog(@"確定");
        } else if (returnCode == NSAlertSecondButtonReturn) {
            NSLog(@"取消");
        } else {
            NSLog(@"else");
        }
    }];
    /*! 方式2
    NSModalResponse response = [alert runModal];
    if (response == NSModalResponseStop) {

    }
    if (response == NSModalResponseAbort) {

    }
    if (response == NSModalResponseContinue) {

    }
    */
}

7. NSView tag

在xib或storyboard中可以設置tag;但是在代碼里猴贰,因為tag為readonly对雪,所以不可以設置tag。

8. layout 無效

[self.layer layoutIfNeeded];
[self.layer layoutSublayers];

9. NSBezierPath 圓角 在drawRect中繪制

https://stackoverflow.com/questions/23094140/nsbezierpath-appendbezierpathwitharcfrompoint-wont-draw
/**
 當前繪圖的上下文為空米绕,因此無法繪制慌植。
 即:只有在drawRect函數(shù)內(nèi)部才提供了繪制圖形上下文環(huán)境,
 不能在非drawRect函數(shù)內(nèi)部通過NSBezierPath或者CGPathRef繪制內(nèi)容
 */
- (void) addArcToProView {
    
    CGSize size = self.proView.bounds.size;
    CGFloat h = size.height;
    CGFloat w = self.tfPro_w.constant / 0.65f;/*重點义郑,計算*/
    NSBezierPath *thePath = [NSBezierPath bezierPath]; // all drawing instruction goes to thePath.
    /*線條*/
    [NSBezierPath setDefaultLineWidth:5.0];
    [[NSColor clearColor] set];/*必須設置為clearColor蝶柿,否則會顯示邊框*/
    [thePath moveToPoint:NSMakePoint(w, h)];
    [thePath lineToPoint:NSMakePoint(0, h)];
    [thePath appendBezierPathWithArcFromPoint:NSMakePoint(0,0)  toPoint:NSMakePoint(h,0) radius:h];
    [thePath lineToPoint:NSMakePoint(w, 0)];
    [thePath lineToPoint:NSMakePoint(w, h)];
    [thePath stroke];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = CGRectMake(0, 0, w, h);
    maskLayer.path = [[LTCGTool tool] getCGPathFromNSBezierPath:thePath];
    self.proView.layer.mask = maskLayer;
}

#pragma mark - NSBezierPath
/// 獲取NSBezierPath的CGPath
/// 也可以使用分類的方式實現(xiàn)
/// @param path NSBezierPath
- (CGMutablePathRef)getCGPathFromNSBezierPath:(NSBezierPath *)path {
    
    CGMutablePathRef cgPath = CGPathCreateMutable();
    NSInteger n = [path elementCount];
    
    for (NSInteger i = 0; i < n; i++) {
        NSPoint ps[3];
        switch ([path elementAtIndex:i associatedPoints:ps]) {
            case NSBezierPathElementMoveTo: {
                CGPathMoveToPoint(cgPath, NULL, ps[0].x, ps[0].y);
                break;
            }
            case NSBezierPathElementLineTo: {
                CGPathAddLineToPoint(cgPath, NULL, ps[0].x, ps[0].y);
                break;
            }
            case NSBezierPathElementCurveTo: {
                CGPathAddCurveToPoint(cgPath, NULL, ps[0].x, ps[0].y, ps[1].x, ps[1].y, ps[2].x, ps[2].y);
                break;
            }
            case NSBezierPathElementClosePath: {
                CGPathCloseSubpath(cgPath);
                break;
            }
            default: NSAssert(0, @"Invalid NSBezierPathElement");
        }
    }
    return cgPath;
}

10. NSScrollView

基本用法

//style,overloy會獨立出來scroll的部分非驮,背景透明
_scrollView.scrollerStyle = NSScrollerStyleOverlay;
// 水平和垂直的彈性屬性
_scrollView.horizontalScrollElasticity = NSScrollElasticityNone;
_scrollView.verticalScrollElasticity = NSScrollElasticityNone;
_scrollView.scrollerInsets = NSEdgeInsetsMake(0, 0, 0, -20);//NSEdgeInsets(top:0, left:0, bottom:0, right:-20)
_scrollView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
_scrollView.backgroundColor = [NSColor clearColor];
_scrollView.drawsBackground = NO;
  1. 設置偏移交汤,也可以給NSScrollView添加extension(Swift)或Category(OC)。
CGFloat pY = 0;
CGFloat h = NSHeight(_scrollView.bounds);
NSView *documentView = self.scrollView.documentView;
if ( NO == documentView.isFlipped ) {
    pY = MAX(NSHeight(_scrollView.bounds), NSHeight(documentView.bounds));
}    
[_scrollView scrollPoint:CGPointMake(0, pY - h + _scrollView.fittingSize.height)];// 設置無效
[documentView scrollPoint:CGPointMake(0, pY)];// 粗略值
[documentView scrollPoint:CGPointMake(0, pY - h + _scrollView.fittingSize.height)];// 更為準確的值
[_scrollView.contentView scrollToPoint:CGPointMake(0, pY - h + _scrollView.fittingSize.height)];// 更為準確的值
  1. NSScrollView與TitleBar重疊時劫笙,top會有偏移芙扎,下列方法清除偏移。
_scrollView.automaticallyAdjustsContentInsets = NO;
//_scrollView.contentInsets = NSEdgeInsetsMake(-44, 0, 0, 0);// 不用
  1. 自定義MYScrollView:禁止滑動填大;嵌套戒洼,滑動事件下傳;
@interface MYScrollView : NSScrollView

@end
@implementation MYScrollView

//- (void)reflectScrolledClipView:(NSClipView *)cView {
// /*禁止滑動失敗*/
//}

- (void)scrollWheel:(NSEvent *)event {
    
//    NSLog(@"%@-%s", NSStringFromClass([self class]), __func__);
//    NSLog(@"%s, %@, %@", __func__, self, @(event.phase));
//    [super scrollWheel:event];/*禁止滑動:重載scrollWheel:,并且不調(diào)用super允华,無任何實現(xiàn)*/
    
    /**嵌套圈浇,滑動事件下傳:NSScrollView內(nèi)嵌套一個MYScrollView,可以隨著外側(cè)的NSScrollView一起滾動*/
    [self.nextResponder scrollWheel:event];
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末靴寂,一起剝皮案震驚了整個濱河市磷蜀,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌百炬,老刑警劉巖褐隆,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件复亏,死亡現(xiàn)場離奇詭異趾疚,居然都是意外死亡骂删,警方通過查閱死者的電腦和手機尺碰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來歇攻,“玉大人固惯,你說我怎么就攤上這事〉舴” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵澳窑,是天一觀的道長斧散。 經(jīng)常有香客問我,道長摊聋,這世上最難降的妖魔是什么鸡捐? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮麻裁,結(jié)果婚禮上箍镜,老公的妹妹穿的比我還像新娘。我一直安慰自己煎源,他們只是感情好色迂,可當我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著手销,像睡著了一般歇僧。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上锋拖,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天诈悍,我揣著相機與錄音,去河邊找鬼兽埃。 笑死侥钳,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的柄错。 我是一名探鬼主播舷夺,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼售貌!你這毒婦竟也來了冕房?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤趁矾,失蹤者是張志新(化名)和其女友劉穎耙册,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體毫捣,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡详拙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年帝际,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片饶辙。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡蹲诀,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出弃揽,到底是詐尸還是另有隱情脯爪,我是刑警寧澤,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布矿微,位于F島的核電站痕慢,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏涌矢。R本人自食惡果不足惜掖举,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望娜庇。 院中可真熱鬧塔次,春花似錦、人聲如沸名秀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽匕得。三九已至熄守,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間耗跛,已是汗流浹背裕照。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留调塌,地道東北人晋南。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像羔砾,于是被迫代替她去往敵國和親负间。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,060評論 2 355

推薦閱讀更多精彩內(nèi)容