[self.window miniaturize:nil]缩小 再放大后 输入框失去焦点

本文介绍了一种解决Mac环境下使用Objective-C编程时,全屏无标题栏窗口在手动缩小后再放大导致的文本输入框失焦问题的方法。通过创建NSWindow的子类并重写canBecomeKeyWindow及canBecomeMainWindow方法来实现。


(以下参考自外网)

问题原因:

    self.window.styleMask = kCGMainMenuWindowLevelKey;

          设置了无标题栏样式,手动调用[self.window miniaturize:nil] 缩小后 再放大,文本输入框出现失去焦点情况

          

解决方法:

          创建一个NSWindow的子类,将xib 的 window关键到此子类,最后在.m里面重写 两个方法如下 :


AppDelegate.m

@property (weak) IBOutlet MyWindow *window;


MyWindow.m      

-(BOOL)canBecomeKeyWindow
{
    return YES;
}

-(BOOL)canBecomeMainWindow
{
    
    return YES;

}

MainMenu.xib





addSubview(messageCenterButton) addSubview(messageNumberLabel)private func makeConstraints() { // closeButton.snp.makeConstraints { make in // make.centerY.equalTo(self) // make.leading.equalTo(self).offset(8) // } // miniaturizeButton.snp.makeConstraints { make in // make.centerY.equalTo(self) // make.leading.equalTo(closeButton.snp.trailing).offset(8) // } // zoomButton.snp.makeConstraints { make in // make.centerY.equalTo(self) // make.leading.equalTo(miniaturizeButton.snp.trailing).offset(8) // } logoButton.snp.makeConstraints { make in make.centerY.equalTo(self) make.leading.equalTo(self).offset(72) } cpuProgressView.snp.makeConstraints { make in make.trailing.equalTo(cpuInfoView.snp.leading).offset(-8) make.centerY.equalTo(self) } cpuInfoView.snp.makeConstraints { make in make.leading.greaterThanOrEqualTo(logoButton.snp.trailing).offset(500) make.centerY.equalTo(self) } memoryProgressView.snp.makeConstraints { make in make.trailing.equalTo(memoryInfoView.snp.leading).offset(-8) make.centerY.equalTo(self) } memoryInfoView.snp.makeConstraints { make in make.leading.equalTo(cpuInfoView.snp.trailing).offset(48) make.centerY.equalTo(self) } seperatorView.snp.makeConstraints { make in make.leading.equalTo(memoryInfoView.snp.trailing).offset(24) make.centerY.equalTo(self) make.width.equalTo(1) make.height.equalTo(24) } downLoadButton.snp.makeConstraints { make in make.leading.equalTo(seperatorView.snp.trailing).offset(24) make.centerY.equalTo(self) } aboutButton.snp.makeConstraints { make in make.leading.equalTo(downLoadButton.snp.trailing).offset(8) make.centerY.equalTo(self) } userButton.snp.makeConstraints { make in make.leading.equalTo(aboutButton.snp.trailing).offset(8) make.trailing.equalTo(self).offset(-24) make.centerY.equalTo(self) } let downloadLabelWidth: CGFloat = 20; let downloadLabelHeigth: CGFloat = 14; downloadingNumberLabel.snp.makeConstraints { make in make.top.equalTo(downLoadButton.snp.top); make.leading.equalTo(downLoadButton.snp.trailing).offset(-(downloadLabelWidth / 2) - 6); make.width.equalTo(downloadLabelWidth); // make.height.equalTo(downloadLabelHeigth); } downloadingNumberLabel.layer?.cornerRadius = (downloadLabelHeigth - 2) / 2; downloadingNumberLabel.layer?.masksToBounds = true; downloadingNumberLabel.layer?.borderWidth = 1.0; downloadingNumberLabel.layer?.borderColor = NSColor.red.cgColor; // 执行一次刷新下载条目方法,用于在登录或切换组织后刷新工具栏角标 TPGuardDownloadManager.shared.loadDownloadItemList() }我新加了一个messagebutton和一个messagelabel,想要的效果是按钮小和下载按钮一样,排列在下载按钮的左边,控制好他和其他控件的间距基本类似于添加前的布局,怎么写约束?
最新发布
09-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值