change UITableView setBackgroundColor if UISearchBar is populated - objective-c

If anyone could help me with this, that would be great. Thanks in advance Max
I want to change UITableView setBackgroundColor from clearColor to whiteColor if text is entered in UISearchBar .
i.e. the placeholder text disappears.
Immediately when text is deleted i.e. placeholder text reappears
the UITableView setBackgroundColor resumes to clearColor.
CGRect frame = CGRectMake(0, 140, 320, 170);
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(13, 100, 300, 40)];
[searchBar setDelegate:self];
[searchBar setPlaceholder:#"Species/Common Name"];
[searchBar setShowsCancelButton:YES];
[searchBar setTintColor:kITBarTint];
[self.view addSubview:searchBar];
self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.tableView setDataSource:self];
[self.tableView setDelegate:self];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.view addSubview:self.tableView];
[self.tableView setTableHeaderView:header];

Use the search bar delegate:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
self.searchDisplayController.searchResultsTableView.backgroundColor = [UIColor grayColor];
}

UISearchBarDelegate Protocol Reference
Use one of the delegate's methods:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
UISearchBar has property text so you can check for user input.
For example:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if([searchText length] > 0)
{
self.tableView.backgroundColor = [UIColor whiteColor];
}
else
{
self.tableView.backgroundColor = [UIColor clearColor];
}
}

I think You should do your changes in UISearchBarDelegate's method - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar

Try to change colors in delegate function of search bar like
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar
(void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
and put the self.tableView.backgroundColor = [UIColor yourcolor];

Related

Cocoa NSAnimationContex Weird behaviour

I have an NSView NSButton and an NSTextField in this view's subviews
all of them are Layer Backed
I ma trying to animate this view changing frame.
- (void)animateTheView:(SPTabView *)view toFrame:(NSRect)endFrame
{
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context)
{
context.allowsImplicitAnimation = YES;
context.duration = ANIMATION_TIME;//is a define 0.6f
[view setFrame:endFrame];
} completionHandler:nil];
}
Edit In my view I am not using AutoLayout I am just overriding the setFrame:
- (void)setFrame:(NSRect)frame
{
[super setFrame:frame];
[_titleLabel setFrame:self.bounds]; //The NSTextField I am talking about
[_closeButton setFrame:NSMakeRect(5, 2, B_WIDTH, B_HEIGHT)];//This one works fine
[self removeTrackingRect:_tracking];
_tracking = [self addTrackingRect:[self bounds] owner:self userData:NULL assumeInside:NO];
}
EDIT 2 Posting the subViews Initialisation Code
[super setWantsLayer:YES];
[self setWantsLayer:YES];
[self setCanDrawSubviewsIntoLayer:YES];
[self drawRegular];
_closeButton = [[NSButton alloc]init];
[_closeButton setCanDrawSubviewsIntoLayer:YES];
[_closeButton setTitle:#"X"];
[_closeButton setTarget:self];
[_closeButton setAction:#selector(closeTab)];
[_closeButton setHidden:YES];
[_closeButton setWantsLayer:YES];
_titleLabel = [[SPLabel alloc]init]; //SPLabel is a silly subclass of NSTextField which just set it's properties like editable to as NO
[_titleLabel setText:#"Untitled.txt"];
[_titleLabel setTextColor:[NSColor blackColor]];
[_titleLabel setAlignment:NSCenterTextAlignment];
[_titleLabel setWantsLayer:YES];
[_titleLabel setTag:6];
[self addSubview:_titleLabel];
[self addSubview:_closeButton];
My view and it's button animating FINE! but the textField doesn't animate at all - it just jumps to the EndFrame...
Any Ideas some1 ?

View Won't Accept First Responder Status

I'm creating a new window and view programmatically. I need the view to accept First Responder status. For whatever reason it's not.
I'm doing this old school because I want my app to be compatible with older versions of OSX. Here's what I've got:
- (IBAction)startShow:(id)sender {
// Capture the main display
if (CGDisplayCapture( kCGDirectMainDisplay ) != kCGErrorSuccess) {
NSLog( #"Couldn't capture the main display!" );
}
// Get the shielding window level
windowLevel = CGShieldingWindowLevel();
// Get the screen rect of our main display
screenRect = [[NSScreen mainScreen] frame];
// Put up a new window
displayWindow = [[ShowWindow alloc] initWithContentRect:screenRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO screen:[NSScreen mainScreen]];
[displayWindow setBackgroundColor:[NSColor blackColor]];
[displayWindow setLevel:windowLevel];
[displayWindow makeKeyAndOrderFront:nil];
// Load show window with options from settings window.
[displayWindow loadOptions:screenRect];
}
Just to clarify, displayWindow is an instance of ShowWindow, which is a subclass of NSWindow. In my ShowWindow class I'm doing the following:
- (BOOL)canBecomeKeyWindow
{
return YES;
}
- (void) loadOptions:(NSRect)screenRect {
// Create custom view to hold text fields
view = [[ShowView alloc] initWithFrame: screenRect];
[self setContentView:view];
// Track Title Text Field Initialize
textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, screenRect.size.height / 4, screenRect.size.width, 350)];
[textField setStringValue:#"My Label"];
[textField setBezeled:NO];
[textField setDrawsBackground:NO];
[textField setEditable:NO];
[textField setSelectable:NO];
[textField setTextColor:[NSColor whiteColor]];
[textField setFont:[NSFont fontWithName:#"Helvetica" size:92]];
[textField setAlignment:NSCenterTextAlignment];
// Add track title text field to main view.
[view addSubview:textField];
// Make the view first responder
[self makeFirstResponder:view];
}
view is an instance of ShowView, which is a subclass of NSView. Last but not least here's what I've got going on in ShowView:
// Next four methods set main view as first responder to accept keyboard input
- (BOOL)acceptsFirstResponder
{
NSLog(#"I accepted being a first responder! Yea!");
return YES;
}
- (BOOL)resignFirstResponder
{
[self setNeedsDisplay:YES];
return YES;
}
- (BOOL)becomeFirstResponder
{
[self setNeedsDisplay:YES];
return YES;
}
- (void)insertText:(NSString *)input
{
}

UIImagePickerController how to hide the flip camera button?

is there a way to hide the flip camera button inside the UIImagePickerController?
thanks for reading
!^_^!
I ended up using a custom subclass of UIImagePickerController to fix this (and other) issues:
#import "SMImagePickerController.h"
#implementation SMImagePickerController
void hideFlipButtonInSubviews(UIView *view) {
if ([[[view class] description] isEqualToString:#"CAMFlipButton"]) {
[view setHidden:YES];
} else {
for (UIView *subview in [view subviews]) {
hideFlipButtonInSubviews(subview);
}
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
hideFlipButtonInSubviews(self.view);
}
#end
You should be able to create an empty button inside an overlayview that you float on top of the flip camera button. I hacked the code below to test and it seemed to work. Give it a try.
UIView *cameraOverlayView = [[UIView alloc] initWithFrame:CGRectMake(screenSize.width - 100.0f, 5.0f, 100.0f, 35.0f)];
[cameraOverlayView setBackgroundColor:[UIColor blackColor]];
UIButton *emptyBlackButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 35.0f)];
[emptyBlackButton setBackgroundColor:[UIColor blackColor]];
[emptyBlackButton setEnabled:YES];
[cameraOverlayView addSubview:emptyBlackButton];
cameraUI.allowsEditing = YES;
cameraUI.showsCameraControls = YES;
cameraUI.delegate = self;
cameraUI.cameraOverlayView = cameraOverlayView;

Editable NSTextField on Mac in Cocos2d project

I'm trying to display editable NSTextField on Mac in Cocos2d project. I run all my test code directly from AppDelegate to make test simple and eliminate all side effects. The code bellow shows red window with nice edit field. The trouble is that the field looks like without focus (text selection is gray) and is not editable (no cursor and no reaction to key strokes). I can change the text selection with mouse but that is all.
Any hint to what's wrong?
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
[glView_ setFrameSize:NSMakeSize(1024,768)];
[director setOpenGLView:glView_];
[director setResizeMode:kCCDirectorResize_AutoScale];
[glView_ setFrameSize:NSMakeSize(window_.frame.size.width,window_.frame.size.height-20)];
[window_ setContentAspectRatio:NSMakeSize(1024,768)];
[window_ setAcceptsMouseMovedEvents:NO];
[self addOverlayWindow];
//[director runWithScene:[MCGameScene scene]/*[HelloWorldLayer scene]*/];
}
- (void) addOverlayWindow;
{
NSRect windowRect = [[window_ contentView] frame] ;
NSWindow* uiWindow = [[NSWindow alloc] initWithContentRect:windowRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered defer:YES];
[uiWindow setBackgroundColor: [NSColor redColor/*clearColor*/]];
[uiWindow setOpaque:NO];
NSView* uiView = [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, windowRect.size.width, windowRect.size.height)] autorelease];
[uiView translateOriginToPoint:NSMakePoint(100, uiView.bounds.size.height/2)];
uiView.wantsLayer = YES;
[uiWindow setContentView:uiView];
NSTextField *textField;
textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 800, 80)];
[textField setFont:[NSFont fontWithName:#"Helvetica Bold" size:60]];
[textField setStringValue:#"My XXXXXXXXX Label"];
[textField setBezeled:YES];
[textField setDrawsBackground:YES];
[textField setEditable:YES];
[textField setSelectable:YES];
[textField setEnabled:YES];
[uiView addSubview:textField];
// None of these combinations works...
// [textField becomeFirstResponder];
// [uiWindow setInitialFirstResponder:textField];
// [uiWindow makeFirstResponder:textField];
//[window_ makeFirstResponder:textField];
[window_ setInitialFirstResponder:textField];
// [uiView setInitialFirstResponder:textField];
// [uiView makeFirstResponder:textField];
[window_ addChildWindow:uiWindow ordered:NSWindowAbove];
}
Thanks in advance for your help,
--Josef

Subview presentation delayed

In my UITableViewController subclass I want to prepare the tabular data and reload the table after the view has already appeared (the table therefore initially loads empty). In my app delegate I have methods to produce and remove an activity screen. My problem seems to be that the activity view presentation is being delayed until after the reloadData call has been made. I proved this by removing the hideActivity line and sure enough my activity view appeared simultaneously with the reloading of the table. Here's the viewDidAppear for my view controller...
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] showActivity];
[self prepare];
[self.tableView reloadData];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] hideActivity];
}
I'm assuming this may have to do with the views not redrawing mid-method, but don't remember this happening before. Any ideas?
...although I know they work, here's my activity methods. Maybe the way I'm doing these allows for some sort of delayed appearance...
- (void)showActivity
{
self.activityView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
self.activityView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.75f];
UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((320.0f / 2.0f) - (37.0f / 2.0f), (480.0f / 2.0f) - (37.0f / 2.0f) - (20.0f / 2.0f), 37.0f, 37.0f)] autorelease];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[spinner startAnimating];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, spinner.frame.origin.y + spinner.frame.size.height + 10.0f, 320.0f, 30.0f)] autorelease];
label.textAlignment = UITextAlignmentCenter;
label.text = #"Working";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[self.activityView addSubview:label];
[self.activityView addSubview:spinner];
[self.window addSubview:self.activityView];
}
- (void)hideActivity
{
[self.activityView removeFromSuperview];
[self.activityView release];
}
Your UI isn't updating until you return control to the runloop. You need to either perform the preparation of the data in a background thread, or return control to the runloop to update the UI before starting the preparation, like this:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] showActivity];
[self performSelector:#selector(prepareData) withObject:nil afterDelay:0.0];
}
- (void)prepareData {
[self prepare];
[self.tableView reloadData];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] hideActivity];
}