UIButton not responding. Why? - iphone

working on an app and using the following code:
- (void)viewDidLoad {
[super viewDidLoad];
dataSource = [[NSArray alloc] initWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"stories.plist"]];
contentsHeaderImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ContentsHeader.png"]] autorelease];
[contentsHeaderImageView setFrame:CGRectMake(0, 0, 320, 131)];
[contentsHeaderImageView setUserInteractionEnabled:YES];
[self.view addSubview:contentsHeaderImageView];
UITableView *tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(contentsHeaderImageView.frame), self.view.frame.size.width, self.view.frame.size.height - CGRectGetMaxY(contentsHeaderImageView.frame))] autorelease];
[tableView setDelegate:self];
[tableView setDataSource:self];
[tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
[tableView setContentInset:UIEdgeInsetsMake(1, 0, 1, 0)];
[tableView setSeparatorColor:[UIColor whiteColor]];
[self.view addSubview:tableView];
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
[tableView setBackgroundColor:[UIColor clearColor]];
UIImageView *newView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"fade.png"]] autorelease];
[newView setFrame:CGRectMake(0, -20, 320, 69)];
[self.view addSubview:newView];
splashImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default.png"]] autorelease];
[splashImage setFrame:CGRectMake(0, -20, 320, 480)];
[self.view addSubview:splashImage];
[self performSelector:#selector(animateOutSplashImage:) withObject:splashImage afterDelay:3];
}
-(void)drawBookmarkButton
{
if (self.bookmarkButton) {
[self.bookmarkButton removeFromSuperview];
}
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"bookmark"]) {
self.bookmarkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.bookmarkButton setImage:[UIImage imageNamed:#"bookmark 1.png"] forState:UIControlStateNormal];
[contentsHeaderImageView addSubview:self.bookmarkButton];
[self.bookmarkButton addTarget:self action:#selector(bookmarkButtonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.bookmarkButton setFrame:CGRectMake(260, 0, 50, 35)];
//UITapGestureRecognizer *gr = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(bookmarkButtonTapped)] autorelease];
//[self.bookmarkButton addGestureRecognizer:gr];
}
}
-(void)animateOutSplashImage:(UIImageView *)splashImg
{
[UIView animateWithDuration:1 animations:^{
[splashImage setAlpha:0];
}completion:^(BOOL finished){
[splashImage removeFromSuperview];
splashImage = nil;
//[self drawBookmarkButton];
}];
}
-(void)viewWillAppear:(BOOL)animated
{
[self drawBookmarkButton];
}
For some reason the first time the app is run the bookmark button won't respond to taps, but if I push a view controller then go back it's working fine. Spent a while trying to figure this out and nothing is working.
Any ideas???
Thanks!

did you try
[contentsHeaderImageView bringSubViewToFront:self.bookmarkButton];
also create a UIView instance for headerView and add imageView and button into the view.

Apologies, it was (as usual with me!) a stupid mistake!
In the delegate I set:
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.translucent = YES;
and must have forgot about it. Setting the nav bar to hidden instead meant that the nav bar wasn't blocking the button.
Thanks for your help!

Related

Something strange with my NavBar

i have an iPhone app with TabBarController that each tab has a Nav Controller.. one of the Nav Controller pushes to another Viewcontroller. in that View Controller i am trying to Hide the back button and place One of my custom buttons. in the
- (void)viewDidLoad
{
[super viewDidLoad];
self._allUsersParticipantsInTheVideo = [[NSMutableArray alloc] init];
self._allUsersInvitesInTheVideo = [[NSMutableArray alloc] init];
for (int i=0; i < [self._curVideoToShow._videoParticipants count]; i++) {
[self._allUsersParticipantsInTheVideo addObject:[self._curVideoToShow._videoParticipants objectAtIndex:i]];
}
for (int i=0; i < [self._curVideoToShow._videoInvitees count]; i++) {
[self._allUsersInvitesInTheVideo addObject:[self._curVideoToShow._videoInvitees objectAtIndex:i]];
}
[self setNavBar];
}
#pragma mark - Set NavBar
-(void)setNavBar{
self.navigationItem.hidesBackButton = YES;
UIView *navBarItemview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navBarItemview setBackgroundColor:[UIColor clearColor]];
// Set cutsom back button
//UIImage *backImage = [UIImage imageNamed:#"backButton.png"];
UIImage *backImage = [UIImage imageNamed:#"LeftArrowBg.png"];
CGRect frame = CGRectMake(0, 6, backImage.size.width + 4, backImage.size.height);
UIButton *bttn = [[UIButton alloc] initWithFrame:frame];
[bttn setBackgroundImage:backImage forState:0];
[bttn setTag:kBackBttn];
[bttn setTitle:#"Back" forState:UIControlStateNormal];
[bttn setTitleEdgeInsets:UIEdgeInsetsMake(4, 5, 0, 0)];
[bttn.titleLabel setFont:[UIFont fontWithName:#"PTSans-Bold" size:12.0]];
[bttn setContentMode:UIViewContentModeScaleAspectFit];
[bttn addTarget:self action:#selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[navBarItemview addSubview:bttn];
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 4, 320, 40)];
[titleLbl setFont:[UIFont fontWithName:#"PTSans-Bold" size:24.0]];
[titleLbl setTextColor:[UIColor whiteColor]];
[titleLbl setTextAlignment:UITextAlignmentCenter];
[titleLbl setBackgroundColor:[UIColor clearColor]];
[titleLbl setTag:kTitleLbl];
[titleLbl setShadowOffset:CGSizeMake(0, 1)];
[titleLbl setShadowColor:[UIColor blackColor]];
[titleLbl setText:#"All Participants"];
[navBarItemview addSubview:titleLbl];
self.navigationItem.titleView = navBarItemview;
}
As you can see i've written :
self.navigationItem.hidesBackButton = YES;
but on the first time when the app is lunched, i see the back button of the NavBar only for the first time, but later on for every time that i open the app the button remains to be my custom one. ( and also when i Navigate it is my custom )
someone?
try with this..
-(void)viewWillAppear:(BOOL)animated
{
self.navigationItem.hidesBackButton = YES;
}
or
[self.navigationController.navigationItem setHidesBackButton:YES];
or
self.navigationController.navigationItem.backBarButtonItem = nil;

Disable keyboard on UITextField on edit

Since when I click the UITextField the date picker is showing along with the keyboard, I want to hide the key board operation on dob-text field?Here my code
- (void)removeViews:(id)object
{
[[self.view viewWithTag:9] removeFromSuperview];
[[self.view viewWithTag:10] removeFromSuperview];
[[self.view viewWithTag:11] removeFromSuperview];
}
- (void)dismissDatePicker:(id)sender
{
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216);
[UIView beginAnimations:#"MoveOut" context:nil];
[self.view viewWithTag:9].alpha = 0;
[self.view viewWithTag:10].frame = datePickerTargetFrame;
[self.view viewWithTag:11].frame = toolbarTargetFrame;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(removeViews:)];
[UIView commitAnimations];
}
- (IBAction)but
{
//[eventText resignFirstResponder];
[dob resignFirstResponder];
if ([self.view viewWithTag:9])
{
return;
}
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);
UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds ];
darkView.alpha = 0;
darkView.backgroundColor = [UIColor blackColor];
darkView.tag = 9;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissDatePicker:)] ;
[darkView addGestureRecognizer:tapGesture];
[self.view addSubview:darkView];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] ;
datePicker.datePickerMode=UIDatePickerModeDate;
datePicker.tag = 10;
[datePicker addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)] ;
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] ;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissDatePicker:)] ;
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];
[UIView beginAnimations:#"MoveIn" context:nil];
toolBar.frame = toolbarTargetFrame;
datePicker.frame = datePickerTargetFrame;
darkView.alpha = 0.5;
[UIView commitAnimations];
//[datePicker addTarget:self action:#selector(dateText:)forControlEvents:UIControlEventValueChanged];
//NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
//_dateFormatter.dateStyle = NSDateFormatterFullStyle;
//dateText.text = [NSString stringWithFormat:#"%#",
// [_dateFormatter stringFromDate:datePicker.date]];
//[self.tableview reloadData];
}
I added the resignfirstresponder also,since its showing the same error
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
return [txt1 resignFirstResponder];//dob textfield
}
Use UITextField's delegate method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == dob-text )
//load picker here
return NO; //hide keyboard
else
return YES; //show keyboard
}
A better solution is to set the UIPickerView as the inputView for the UITextField.
And you can set the UIToolBar as the inputAccessoryView of the inputView.
This way iOS will handle the displaying of all UIPickView.
Just set the properties top the correct views:
self.dateTextField.inputView = self.datePicker;
self.dateTextField.inputAccessoryView = self.inputToolbar;
On another note, getting views with viewWithTag: methods means that the you are looping thru all the views. Why not create properties for these views. It will be less messy and might be faster.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
datePicker.hidden = NO;
[textField resignFirstResponder];
}
try in textFieldDidBeginEditing
-(void) textFieldDidBeginEditing:(UITextField *)textField{
[textField resignFirstResponder];
}
First of all a textfield should not have a popover - neither picker nor popover nor anything
Best solution would be to put a button - type custom - set image like a date selector image and add selector - in that case you would n't have any issues w.r.t textfields popover and the look would be good as well and you can customize it as per your requirement.

How to detect both main view UILongPressGestureRecognizer and subview UIPanGestureRecognizer

I encountered a problem with my UILongPressGestureRecognizer and UIPanGestureRecognizer.
i wish to long press in my self.view to add a view with UIPanGestureRecognizer in my self.view.
However the UIPanGestureRecognizer is not recognizing.
did i missed anything ?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//[self initImagesAndGesture];
UILongPressGestureRecognizer *tapRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(addImgView:)];
tapRecognizer.numberOfTouchesRequired = 1;
tapRecognizer.minimumPressDuration = 0.7;
[tapRecognizer setDelegate:self];
self.view.userInteractionEnabled = YES;
[self.view addGestureRecognizer:tapRecognizer];
}
-(void)addImgView:(UILongPressGestureRecognizer *)recognizer
{
NSLog(#"tappppp");
if(UIGestureRecognizerStateBegan == recognizer.state) {
// Called on start of gesture, do work here
NSLog(#"UIGestureRecognizerStateBegan");
UIImage *img = [UIImage imageNamed:#"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
UILabel *timeStamp = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
timeStamp.text = [NSString stringWithFormat:#"%f",[NSDate date]];
UIView *drinkView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[drinkView setUserInteractionEnabled:YES];
CGPoint tapLocation = [recognizer locationInView:recognizer.view];
[timeStamp setCenter:CGPointMake(tapLocation.x, tapLocation.y)];
[imgView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];
[drinkView addSubview:imgView];
[drinkView addSubview:timeStamp];
[drinkView setUserInteractionEnabled:YES];
[imgView setUserInteractionEnabled:YES];
[timeStamp setUserInteractionEnabled:YES];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[recognizer1 setDelegate:self];
[drinkView addGestureRecognizer:recognizer1];
[self.view addSubview:drinkView];
}
}
-(void)addImgView:(UILongPressGestureRecognizer *)recognizer
{
NSLog(#"tappppp");
if(UIGestureRecognizerStateBegan == recognizer.state) {
// Called on start of gesture, do work here
NSLog(#"UIGestureRecognizerStateBegan");
UIImage *img = [UIImage imageNamed:#"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
UILabel *timeStamp = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
timeStamp.text = [NSString stringWithFormat:#"%f",[NSDate date]];
UIView *drinkView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 75,115)];
drinkView.backgroundColor=[UIColor redColor];
[drinkView setUserInteractionEnabled:YES];
CGPoint tapLocation = [recognizer locationInView:recognizer.view];
[drinkView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];
[drinkView setUserInteractionEnabled:YES];
[imgView setUserInteractionEnabled:YES];
[timeStamp setUserInteractionEnabled:YES];
[drinkView addSubview:imgView];
[drinkView addSubview:timeStamp];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[recognizer1 setDelegate:self];
[drinkView addGestureRecognizer:recognizer1];
[self.view addSubview:drinkView];
}
if(UIGestureRecognizerStateChanged == recognizer.state) {
// Do repeated work here (repeats continuously) while finger is down
NSLog(#"UIGestureRecognizerStateChanged");
}
if(UIGestureRecognizerStateEnded == recognizer.state) {
// Do end work here when finger is lifted
NSLog(#"UIGestureRecognizerStateEnded");
}
}

How to add Date picker in Action Sheet?

- (IBAction) showCatPicker {
if (self.catList !=nil) {
self.catList=nil;
[catList release];
}
self.catList = [[NSMutableArray alloc] init];
self.actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
if(self.catPicker == nil) {
self.catPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
self.catPicker.showsSelectionIndicator = YES;
self.catPicker.dataSource = self;
self.catPicker.opaque = YES;
self.catPicker.multipleTouchEnabled = YES;
self.catPicker.userInteractionEnabled = YES;
self.catPicker.delegate = self;
}
[self.actionSheet addSubview:self.catPicker];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Select"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor colorWithRed:19.0/255 green:122.0/255 blue:53.0/255 alpha:1.0];
[closeButton addTarget:self action:#selector(dismissGenderPicker:) forControlEvents:UIControlEventValueChanged];
UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Cancel"]];
cancelButton.momentary = YES;
cancelButton.frame = CGRectMake(10, 7.0f, 50.0f, 30.0f);
cancelButton.segmentedControlStyle = UISegmentedControlStyleBar;
cancelButton.tintColor = [UIColor colorWithRed:19.0/255 green:122.0/255 blue:53.0/255 alpha:1.0];
[cancelButton addTarget:self action:#selector(cancelActionSheet) forControlEvents:UIControlEventValueChanged];
[self.actionSheet addSubview:cancelButton];
[self.actionSheet addSubview:closeButton];
[cancelButton release];
[closeButton release];
[self.actionSheet showInView:self.view];
[self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
[catPicker reloadComponent:0];
}
You should definitely NOT use UIActionSheet to hold an UIDatePicker because this functionality is deprecated!
From the Documentation:
Subclassing Notes
UIActionSheet is not designed to be subclassed, nor should you add
views to its hierarchy. If you need to present a sheet with more
customization than provided by the UIActionSheet API, you can create
your own and present it modally with
presentViewController:animated:completion:.
and
Important: UIActionSheet is deprecated in iOS 8. (Note that
UIActionSheetDelegate is also deprecated.) To create and manage action
sheets in iOS 8 and later, instead use UIAlertController with a
preferredStyle of UIAlertControllerStyleActionSheet.
What you could very easily do is to create an UIView to hold the UIDatePicker and animate the view as appropriate. You can even add an UIToolbar to it if you need to.
Here's an example:
Create two properties:
#property (strong, nonatomic) UIDatePicker *theDatePicker;
#property (strong, nonatomic) UIView *pickerView;
Create your picker, embed it into the UIView and show:
-(void) createDatePickerAndShow {
UIToolbar *controlToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, pickerView.bounds.size.width, 44)];
[controlToolbar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:#"Set" style:UIBarButtonItemStyleDone target:self action:#selector(dismissDateSet)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelDateSet)];
[controlToolbar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil] animated:NO];
[theDatePicker setFrame:CGRectMake(0, controlToolbar.frame.size.height - 15, theDatePicker.frame.size.width, theDatePicker.frame.size.height)];
if (!pickerView) {
pickerView = [[UIView alloc] initWithFrame:theDatePicker.frame];
} else {
[pickerView setHidden:NO];
}
CGFloat pickerViewYpositionHidden = self.view.frame.size.height + pickerView.frame.size.height;
CGFloat pickerViewYposition = self.view.frame.size.height - pickerView.frame.size.height;
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYpositionHidden,
pickerView.frame.size.width,
pickerView.frame.size.height)];
[pickerView setBackgroundColor:[UIColor whiteColor]];
[pickerView addSubview:controlToolbar];
[pickerView addSubview:theDatePicker];
[theDatePicker setHidden:NO];
[self.view addSubview:pickerView];
[UIView animateWithDuration:0.5f
animations:^{
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYposition,
pickerView.frame.size.width,
pickerView.frame.size.height)];
}
completion:nil];
}
And to dismiss the DatePicker:
-(void) cancelDateSet {
CGFloat pickerViewYpositionHidden = self.view.frame.size.height + pickerView.frame.size.height;
[UIView animateWithDuration:0.5f
animations:^{
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYpositionHidden,
pickerView.frame.size.width,
pickerView.frame.size.height)];
}
completion:nil];
}
self.view.userInteractionEnabled = NO;
self.blurView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.blurView setBackgroundColor:[UIColor lightGrayColor]];
[self.blurView setAlpha:0.3];
[[[UIApplication sharedApplication] delegate].window addSubview:self.blurView];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:pickerFrame];
pickerView.datePickerMode = UIDatePickerModeDate;
[pickerView setBackgroundColor:[UIColor whiteColor]];
[pickerView addTarget:self action:#selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
__block CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, 220);
self.viewForPicker = [[UIView alloc] initWithFrame:frame];
[self.viewForPicker setBackgroundColor:[UIColor whiteColor]];
[self.viewForPicker addSubview:pickerView];
[[[[UIApplication sharedApplication] delegate] window] addSubview:self.viewForPicker];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[button setTitle:#"Done" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(250, 15, 60, 30)];
[button.layer setCornerRadius:5.0];
[button.layer setBorderWidth:1.0];
[button.layer setBorderColor:[[IMAppStyle appColor] CGColor]];
[self.viewForPicker addSubview:button];
[self.viewForPicker.layer setCornerRadius:8.0];
float animationDuration = 0.25;
[UIView animateWithDuration:animationDuration animations:^{
frame.origin.y -= (180+40+35);
[self.viewForPicker setFrame:frame];
}];

How to make stationary background view in UITableView?

I'm trying to add a view behind my grouped UITableView. Unfortunately, whenever I scroll, the background view moves as well. This happens even when I use [self.view insertSubview:backgroundView belowSubview:_tableView], or [_tableView setBackgroundView:backgroundView]. Why is this background view scrolling? Also, why does my tableView scroll, even though I have disabled scrolling? Are these related?
In app Delegate:
History *historyController = [[History alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:historyController];
In History.m:
- (void)viewDidLoad {
CGRect frame = self.view.frame;
frame.origin.x = 0;
frame.origin.y = 0;
_tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView setScrollEnabled:NO];
[_tableView setBackgroundColor:[UIColor clearColor]];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
UIView *bg = [[UIView alloc] initWithFrame:frame];
[bg setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CenterFade.png"]];
iv.alpha = .750;
[bg addSubview:iv];
[iv release]; iv = nil;
[self.view addSubview:bg];
[self.view addSubview:_tableView];
[bg release]; bg = nil;
[_tableView release];
[super viewDidLoad];
}
Try
- (void)viewDidLoad {
CGRect frame = self.view.frame;
frame.origin.x = 0;
frame.origin.y = 0;
UIView *bg = [[UIView alloc] initWithFrame:frame];
[bg setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CenterFade.png"]];
iv.alpha = .750;
[bg addSubview:iv];
[iv release];
iv = nil;
_tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView setScrollEnabled:NO];
[_tableView setBackgroundColor:[UIColor clearColor]];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[bg addSubview:_tableView];
[_tableView release];
_tableView = nil;
[self.view addSubview:bg];
[bg release];
bg = nil;
[super viewDidLoad];
}
If your controller is an UITableViewController just change it to an UIViewController<UITableViewDelegate, UITableViewDatasource> with a tableView property (It's the same)
I don't know why this is happening, but for some reason self.view is a UITableView and not a UIView. Creating a new UIView and setting it to self.view fixed the problem. I am not using a UITableViewController, but a UIViewController. No idea where the UITableView is coming from!
set [_tableView setBackgroundColor:[UIColor clearColor]];
and then put your background view under UITableView in UIViewController view hierarchy.
UITableView has a backgroundView property.
Swift:
var backgroundView: UIView?
Objective-C
#property(nonatomic, readwrite, retain) UIView *backgroundView
This view will not be moved when you scroll the tableView