问题描述
我的应用程序有两个不同的状态,每个状态都将用一个NSView表示.
My app has two different states, and every state will be representing with a NSView.
所以一次只显示一个视图,问题是当我在视图之间切换时,在我手动调整窗口大小之前,应用程序不会显示新状态!
so there is only one view showed at a time, the problem is when I switched between the views the app doesn't show the new state until I resize the window manually!
我搜索了这个问题,并提出了多个解决方案,但没有任何解决方法:
I searched about this problem and come with more than one solution but nothing worked with me:
[myView setNeedsDisplay:YES];
[myView display];
[[myView.window contentView] setNeedsDisplay:YES];
[mySubView1 setHidden:YES]; || [mySubView1 removeFromSuperView];
我什至将myView定义为Outlet,并且没有任何作用.
I even defined myView as Outlet, and nothing work.
这是我的代码
if (appState == 1) {
[self.splitView setFrameSize:CGSizeMake(self.splitView.frame.size.width, self.view.frame.size.height - 250)];
[self.mySubView1 setHidden:NO];
[self.mySubView2 setHidden:YES];
[self.mySubView2 removeFromSuperview];
[self.mySubView1 addSubview:self.inCallView];
}
else
{
[self.splitView setFrameSize:CGSizeMake(self.splitView.frame.size.width, self.view.frame.size.height - 70)];
[self.mySubView1 setHidden:YES];
[self.mySubView2 setHidden:NO];
[self.mySubView1 removeFromSuperview];
[self.mySubView2 addSubview:self.chatHeaderView];
}
// I need to redraw here
[self.view setNeedsDisplay:YES];
[self.mySubView1 setNeedsDisplay:YES];
[self.mySubView2 setNeedsDisplay:YES];
// and nothing happened until I resize my window manually
推荐答案
我发现了,代码很好,不需要调用任何重绘方法,唯一的问题任何UI操作都需要在主线程
I found it, the code is just fine and no need to call any redraw method, the only problem Any UI action need to be done in the MAIN thread
因此最终代码将是:
dispatch_async( dispatch_get_main_queue(), ^{
if (appState == 1) {
[self.splitView setFrameSize:CGSizeMake(self.splitView.frame.size.width, self.view.frame.size.height - 250)];
[self.mySubView1 setHidden:NO];
[self.mySubView2 setHidden:YES];
}
else
{
[self.splitView setFrameSize:CGSizeMake(self.splitView.frame.size.width, self.view.frame.size.height - 70)];
[self.mySubView1 setHidden:YES];
[self.mySubView2 setHidden:NO];
}
});
谢谢,伙计们.
这篇关于强制以编程方式重新绘制NSView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!