Setting the same background for all Views


#1

Hi folks!

The “backgroundTapped:” method does not work with my code, because I placed a UITableView as a subview in front of each main view to achieve a similar background for all screens - by using the following code:

- (void) viewDidLoad { [super viewDidLoad]; UITableView *tv = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; [[self view] addSubview:tv]; [[self view] sendSubviewToBack:tv]; }

Therefore, the “backgroundTapped:” is never called, because the user can’t touch the view. What’s an elegant way of ensuring the proper function of “backgroundTapped:” and still get the same background “color” for each screen?

Thank you very much!

DetailViewController.h

#import <UIKit/UIKit.h>

@class BNRItem;

@interface DetailViewController : UIViewController <UITextFieldDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{

    __weak IBOutlet UITextField *nameField;
    __weak IBOutlet UITextField *serialNumberField;
    __weak IBOutlet UITextField *valueField;
    __weak IBOutlet UILabel *dateLabel;
    __weak IBOutlet UIImageView *imageView;
}

@property (nonatomic, strong)BNRItem *item;

- (IBAction)changeDate:(id)sender;
- (IBAction)takePicture:(id)sender;
- (IBAction)backgroundTapped:(id)sender;


@end

DetailViewController.m

#import "DetailViewController.h"
#import "BNRItem.h"
#import "ChangeDateController.h"
#import "BNRImageStore.h"

@implementation DetailViewController

@synthesize item;

- (void) viewDidLoad
{
    [super viewDidLoad];
    UITableView *tv = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [[self view] addSubview:tv];
    [[self view] sendSubviewToBack:tv];
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear];
    
    [nameField setText:[item itemName]];
    [serialNumberField setText:[item serialNumber]];
    [valueField setText:[NSString stringWithFormat:@"%d", [item valueInDollars]]];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    
    [dateLabel setText:[dateFormatter stringFromDate:[item dateCreated]]];
    
    NSString *imageKey = [item imageKey];
    
    if (imageKey){
        UIImage *imageToDisplay = [[BNRImageStore sharedStore]imageForKey];
        
        [imageView setImage:imageToDisplay];
    } else {
        [imageView setImage:nil];
    }
    
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear];
    
    [[self view] endEditing:YES];
    
    [item setItemName:[nameField text]];
    [item setSerialNumber:[serialNumberField text]];
    [item setValueInDollars:[[valueField text]intValue]];
}

- (void) setItem:(BNRItem *)i
{
    item = i;
    [[self navigationItem] setTitle:[item itemName]];
}

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


- (IBAction)changeDate:(id)sender {
    
    ChangeDateController *cdc = [[ChangeDateController alloc]init];
    
    [cdc setItem:item];
    
    [[self navigationController] pushViewController:cdc
animated:YES];
    
}

#pragma mark - Camera

- (IBAction)takePicture:(id)sender {
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    } else {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    
    [imagePicker setDelegate:self];
    
    [self presentViewController:imagePicker animated:YES completion:nil];
}

- (IBAction)backgroundTapped:(id)sender {
    
    [[self view] endEditing:YES];
}

- (void) imagePickerController:(UIImagePickerController *)picker
 didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *oldKey = [item imageKey];
    
    if(oldKey){
        [[BNRImageStore sharedStore] deleteImageForKey:oldKey];
    }
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
    
    CFStringRef newUniqueIDString = CFUUIDCreateString (kCFAllocatorDefault, newUniqueID);
    
    NSString *key = (__bridge NSString *)newUniqueIDString;
    [item setImageKey:key];
    
    [[BNRImageStore sharedStore] setImage:image
                                   forKey:[item imageKey]];
    
    CFRelease(newUniqueIDString);
    CFRelease(newUniqueID);
    
    [imageView setImage:image];
    
    [self dismissViewControllerAnimated:YES completion:nil];


}
@end

#2

Try setting the background color of a view either programmatically or in IB.


#3

Thank you very much! I’ve read that the color of an UITableView (blue/white stripes) isn’t a simple background color anymore since the last update and can’t be set like other colors like for example red. But maybe I’m missing another method, I will keep looking. Thank you! :slight_smile: