addSubview not displayed until windows focus has been lost and recovered - xcode

I have the following code attached to an NSButton
- (IBAction) onTest: (id) sender
{
NSLog(#"OnTest");
[overlayView setFrame: scrollView.frame];
[scrollView.superview addSubview: overlayView];
}
My nib is setup as the following picture:
The scrollView outlet is attached to the NSScrollView that encloses a NSTableView and the overlayView is attached to a NSView that has a green background.
On the click of a button, I receive the OnTest log message in xcode, but the green view is not visible, however if I switch focus from the applications window to say XCode, then back to the window, the green overlay is now visible.
I've tried using [scrollView.superView setNeedsDisplay: YES], but that still does not cause the green view to be displayed.
Any suggestions as to that I'm doing wrong?

Related

ViewController canBecomeFirstResponder iOS 8

I have a VC that has an inputAccessoryView that is used to display a textfield (much like the messages app). When I push this view onto the navigation stack everything works fine and by that I mean that the tableview adjusts its insets to make sure nothing scrolls underneath that accessory view. However, if from that view I push on another instance of the same view controller class the insets will not be adjusted and the scrolling of the table will be behind the accessory view.
This issue is seen in iOS 8 only. The other interesting thing about this is that if you then click in the accessory view to open the keyboard the insets are adjusted properly for the keyboard being visible and again when it's hidden.
Also if you don't click the text field to fix the issue and hit back the previous VC is broken as well.
I'm fairly certain based on the information above that this is an iOS 8 bug. I'm hoping someone has seen this and come up with semi reasonable fix.
Nasty solution but a solution nonetheless:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.inputAccessoryView.inputAccessoryTextField becomeFirstResponder];
[self.inputAccessoryView.inputAccessoryTextField resignFirstResponder];
});
}
This allows the view to redraw the insets

How NOT to change the background when NSButton clicked

I have a NSButton on a view I subclass the NSView and draw something in the drawRect:, and the view is belong to a NSWindow which
Then when I click the button, I find the view become transparent, which I guess is because the button click changes the button's background. So I subclass the NSButton,
- (void)mouseDown:(NSEvent *)theEvent {
[self.window.contentView setNeedsDisplay:YES];
[super mouseDown:theEvent];
[self.window.contentView setNeedsDisplay:YES];
}
Right now when I mouseDown and mouseUp, the view seems right, except there is a flash transparent when I click, even more, when I mouseDown and move out of the button, the background becomes transparent again.
Can anyone tell me what should I do to make the background of a clicked button do not change?or where to put the redraw code.
THANKS!

cocoa: NSView touch event

I created a simple cocoa project, and added an NSButton in the window.
Then I added an NSScrollView to the window and hided the NSButton.
However, when I click the scroll view , it is strange that the NSButton action responds!
I guess there is something with the touch event chains, but I failed to find it.
For example, I try to use:
- [NSView becomeFirstResponder];
- [NSView setAcceptsTouchEvents:];
SO what I want is the only the front-most view to become the first responder, and the touch event will not be sent to its superview or so.
Thanks.
This is the view hierarchy:
the scroll view and button are both added to the window view, and the scrollview's frame includes the button's frame. In other words, the button is hidden by the scroll view but still receives click events.
You need to add mouseDown: event in NSScrollView or NSCrollView's View. like this:
-(void)mouseDown:(NSEvent *)theEvent {
NSLog(#"MouseDown in NSView");
}

iPad - Section of screen no longer receiving touch events after full screen modal view controller dismissed on iPad

I have toolbar at the top of the (full screen) main view in my iPad. The toolbar has a barButtonItem that when pressed, shows a popover.
In the popover, I have a UIButton that when pressed, tells the delegate (the main view controller) to dismiss the popover and show a full page modal view.
This all works fine.
When I dismiss the modal view, the area of the screen the popover occupied - including the main view and toolbar buttons - is no longer responding to touch events.
This problem corrects itself if I rotate the device and only occurs in Landscape mode.
Any suggestions?
Update: This bug does not happen when running in the Simulator, only on an actual iPad.
The delegate method I have to dismiss the full screen modal view..
- (void)fullScreenViewControllerDidFinish:(FullScreenWebViewController *)fullScreenWebView {
[self dismissViewControllerAnimated:YES completion:^{
[self setFullScreenWVC:nil];
[[self view] setNeedsLayout]; //Does't fix the issue
}];
}
Update:
Using Instruments, I've gotten the iPad to show me how it's laying out subviews. It looks like it thinks the iPad is in Portrait when the modal view is dismissed but the device is obviously in landscape.
I fixed the problem.
I was using
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
instead of
UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
The iPad wasn't getting the right orientation for some reason.
Thanks for your help.
I may be misunderstanding you issue, but I would suggest trying to call "setNeedsLayout" in your main view once the modal view is dismissed. This should trigger the auto resize of your view layout and may resolve the issue if it has not auto resized.
Take a look at this link for more information:
http://developer.apple.com/library/ios/ipad/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/setNeedsLayout

Change NSView background color when window has focus

I have noticed when an apps window contains an Outline view (such as XCode) it changes color when that window is in focus. With XCode for example, if the window is current then the outline view has a blueish background, if it looses focus it goes grey,
Can anyone help me to replicate this? I presume its something to do with drawRect: but can only manage to get the color to change when the window loads.
Maybe its a built in function and I'm just missing something?
All you have to do in your -drawRect: is check whether the window has main status and draw accordingly:
- (void)drawRect:(NSRect)rect
{
if ([[self window] isMainWindow]) {
// draw active appearance
} else {
// draw inactive appearance
}
}
A window's delegate gets messages whenever a window gets or resigns main or key window status. You can implement the appropriate methods (like -windowDidBecomeMain: and -windowDidResignMain:) in your window delegate to update the window and its subviews as necessary.

Resources