How to make stationary background view in UITableView? - iphone

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

Related

TapGestureRecognizer does not get called

Hi in the main WiewController in the viewDidLoad i set up a
UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(processTap)];
and then i a for loop i create UIViews and add them to a scroll view, that is then added to the main view.
UIView *newsContainer = [[UIView alloc] initWithFrame:CGRectMake(160 * countnews, 30, 156, 126)];
newsContainer.tag = countnews;
newsContainer.userInteractionEnabled = YES;
[newsContainer addGestureRecognizer:recognizer];
[tempScroll addSubview:newsContainer];
then i have a function
- (void)processTap:(UITapGestureRecognizer *)recognizer {
UIView *view = recognizer.view;
NSLog(#"HELLO, %d", view.tag);
}
Which never gets called, any suggestions? your help would be greatly appreciated. Thanks in advance.
here is the entire .m
#import "iPadMainViewController.h"
#import "GradeintView.h"
#import <QuartzCore/CALayer.h>
#import "Category.h"
#import "News.h"
#import "LazyImageView.h"
#import "TouchView.h"
#interface iPadMainViewController ()
#end
#implementation iPadMainViewController
#synthesize detailsView = _detailsView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(processTap:)];
[recognizer setDelegate:self];
GradeintView *MainTitle = [[GradeintView alloc] initWithFrame:CGRectMake(0, 0, 1024, 50)];
GradeintView *MainSubTitle = [[GradeintView alloc] initWithFrame:CGRectMake(0, 50, 1024, 30)];
NSMutableArray *categoriesCollection = [[Category alloc] allCategoriesFromFeedAtUrl:#"http://geonews.ge/xml/category.php"];
UIScrollView *categories = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 512, 768)];
_detailsView = [[UIWebView alloc] initWithFrame:CGRectMake(500, 0, 512, 768)];
[_detailsView addGestureRecognizer:recognizer];
[categories setScrollEnabled:TRUE];
[categories setContentSize:CGSizeMake(500, categoriesCollection.count * 156)];
MainTitle.layer.masksToBounds = NO;
MainTitle.layer.shadowOffset = CGSizeMake(3, 3);
MainTitle.layer.shadowRadius = 5;
MainTitle.layer.shadowOpacity = 0.3;
[categories setBackgroundColor:[UIColor redColor]];
int counter = 0;
for (Category *cat in categoriesCollection)
{
UIView *categoryTitle = [[UIView alloc] initWithFrame:CGRectMake(0, 166 * counter
, 500, 20)];
UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 20)];
[categoryLabel setBackgroundColor:[UIColor clearColor]];
NSMutableArray *allCurrentNews = [[News alloc] allNewsFromCategory:cat.CategoryId];
categoryLabel.text = cat.Name;
categoryLabel.textColor = [UIColor whiteColor];
[categoryTitle addSubview:categoryLabel];
UIColor *myblack = [[UIColor alloc] initWithRed:0.14 green:0.14 blue:0.14 alpha:1];
UIColor *ligheterBlac = [[UIColor alloc] initWithRed:0.227 green:0.22 blue:0.22 alpha:1];
[categoryTitle setBackgroundColor:myblack];
UIScrollView *tempScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 166 * counter, 500, 166)];
UIColor *tempcolor = ligheterBlac;
tempScroll.layer.borderColor = [UIColor colorWithRed:0.34 green:0.34 blue:0.34 alpha:1].CGColor;
tempScroll.layer.borderWidth = 0.5f;
int countnews = 0;
for (News *news in allCurrentNews)
{
UIView *newsContainer = [[UIView alloc] initWithFrame:CGRectMake(160 * countnews, 30, 156, 126)];
newsContainer.tag = countnews;
[newsContainer addGestureRecognizer:recognizer];
//newsContainer.NewsId = news.NewsId;
LazyImageView *image = [[LazyImageView alloc] initWithURl:[NSURL URLWithString:news.Thumbnail]];
image.frame = CGRectMake(0, 0 , 156, 96);
UILabel *newsTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 96, 156, 30)];
newsTitle.backgroundColor = myblack;
newsTitle.numberOfLines = 2;
newsTitle.font = [UIFont systemFontOfSize:11];
newsTitle.text = news.Title;
newsTitle.textColor = [UIColor whiteColor];
newsTitle.textAlignment = UITextAlignmentCenter;
newsContainer.layer.borderColor = [UIColor colorWithRed:0.34 green:0.34 blue:0.34 alpha:1].CGColor;
newsContainer.layer.borderWidth = 0.5f;
[newsContainer addSubview:image];
[newsContainer addSubview:newsTitle];
countnews ++;
[tempScroll setContentSize:CGSizeMake(allCurrentNews.count * 156, 96)];
[tempScroll addSubview:newsContainer];
//[image release];
}
[tempScroll setBackgroundColor: tempcolor];
[categories addSubview:tempScroll];
[categories addSubview:categoryTitle];
[tempcolor release];
[tempScroll release];
counter ++;
}
self.detailsView.layer.masksToBounds = NO;
self.detailsView.layer.shadowOffset = CGSizeMake(-10, 5);
self.detailsView.layer.shadowRadius = 5;
self.detailsView.layer.shadowOpacity = 0.3;
[self.detailsView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.amazon.com"]]];
[self.view addSubview:categories];
[self.view addSubview:self.detailsView];
[self.view addSubview:MainSubTitle];
[self.view addSubview:MainTitle];
}
- (void)processTap:(UITapGestureRecognizer *)recognizer {
UIView *view = recognizer.view;
NSLog(#"HELLO, %d", view.tag);
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)dealloc {
[super dealloc];
}
- (void)loadDetailedContent:(NSString *)s
{
}
#end
Change
initWithTarget:self.view
to
initWithTarget:self
EDIT:
You also have forgotten colon:
initWithTarget:self action:#selector(processTap:)
EDIT2:
You have created _detailsView (with assigned UITapGestureRecognizer) but have not added it to any subview. How it will work?
I think the problem is that the scrollView, which contains your views, has its own internal gesture recognizer that is "taking away" touch events from your tap gesture recognizer. Try implementing the following gesture recognizer delegate method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Have you checked the Interaction setting?
Interaction shall be set to "User Interaction Enabled" in the Attributes inspector for the image.
Change #selector(processTap) to #selector(processTap:)
Because now you calling method that doesn't exist.
try this code
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(processTap)];
[newsContainer addGestureRecognizer::gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;
I assume the problem is, you might have missed to add <UIGestureRecognizerDelegate> in the header file.
You're adding a UIGestureRecognizer to a UIWebview, which is not recommended. UIWebview has its ownUIGestureRecognizer`s which are tricky to over-ride. See this SO question for more info.
you can first add your gestureRecognizer to self.view and check your method called or not... as well check this thread
Issue with a UITapGestureRecognizer

change uitabbar background color

I am write this code but the warning is show "insertSubview" may not respond
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIImage *i = [UIImage imageNamed:#"GO-21-TabBarColorx49.png"];
UIColor *c = [[UIColor alloc] initWithPatternImage:i];
v.backgroundColor = c;
[c release];
[[self tabBar] addSubview:v];
[v release];
}
Did you try
[[self tabBar] insertSubview:v atIndex:0];
Instead of addSubview:
Source: Changing Tint / Background color of UITabBar

How do I put viewController's into a UIScrollView

I want to initialize 5 viewController's that I want to be able to flick between in a UIScrollView, when my app loads.
Here is an example of how you can do this:
- (void)viewDidLoad
{
//standard UIScrollView is added
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:scrollView];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(320*2, 460); //this must be the appropriate size!
//required to keep your view controllers around
controllers = [[NSMutableArray alloc] initWithCapacity:0];
//just adding two controllers
LabeledViewController *one = [[LabeledViewController alloc] initWithPosition:0 text:#"one"];
[scrollView addSubview:one.view];
[controllers addObject:one];
LabeledViewController *two = [[LabeledViewController alloc] initWithPosition:1 text:#"two"];
[scrollView addSubview:two.view];
[controllers addObject:two];
}
LabeledViewController is pretty simple, but you can add as much to it as you want:
#implementation LabeledViewController
- (id)initWithPosition:(NSInteger)position text:(NSString*)text
{
if (self = [super init]) {
myPosition = position;
myText = [text retain];
}
return self;
}
- (void)viewDidLoad
{
//this will setup the position in the UIScrollView
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(320*myPosition, 0, 320, 460)];
self.view = view;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 320, 50)];
label.text = myText;
[self.view addSubview:label];
}

Adding a UIButton to UIScrollView

I am trying to add the UIButton to UIScrollView.
I have added the UIImageView with background image *(size: 320 * 620)*.
Then I added this UIImageView to UIScrollView, and It works fine.
But now I want to add UIButton at position at : (60, 500); (below screen that would appear after scrolling).
I have tried following code, but the button is added on UIView not on scrollview. Button is not getting displayed on the top.
Code :
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden = TRUE;
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SingleModelVar.png"]];
self.imageView = tempImageView;
[tempImageView release];
imageView.userInteractionEnabled = YES;
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; //fit to screen
//scrollView.delaysContentTouches = NO;
scrollView.contentSize = CGSizeMake(320, imageView.frame.size.height); //imageView.frame.size.height=620 here
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
//The Problem begins ..........
btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];
[scrollView addSubview:btnStartDate];
//[self.view addSubview:btnStartDate];
btnEndDate=[[UIButton alloc] initWithFrame:CGRectMake(60,530,100,20)];
[scrollView addSubview:btnEndDate];
//[self.view addSubview:btnEndDate];
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];
}
it is not displayed at the top because you added them before you added the imageView.
Put [scrollView addSubview:imageView]; before btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];

viewing tableViewLines on background of custom UIView

I have a viewController that has a tableView and a custom loading UIView that displays a loading message and spinner while the data of the tableView is being loaded.
The custom UIView has a red background that i can see, but i can still see the lines of the tableView, How can i get to display the custom uiview without seeing the tableView lines on the background.
#implementation LoadingView
#synthesize spinner;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
[self setUserInteractionEnabled:NO];
[self setBackgroundColor:[UIColor blueColor]];
// Spinner
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setCenter:CGPointMake(320/2, 150)];
[self addSubview:spinner];
[spinner startAnimating];
//title label
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"Loading...";
titleLabel.font = [UIFont boldSystemFontOfSize:18];
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor blackColor];
titleLabel.textAlignment = UITextAlignmentLeft;
CGFloat height = [titleLabel.text sizeWithFont: titleLabel.font constrainedToSize: CGSizeMake(300, 1500) lineBreakMode: UILineBreakModeWordWrap].height;
[titleLabel setFrame: CGRectMake(120, 180, 100, height)];
[self addSubview:titleLabel];
[titleLabel release];
}
return self;
}
- (void)drawRect:(CGRect)rect {
}
- (void)dealloc {
[super dealloc];
[spinner release];
}
#end
You can set the lines "off", when you start you loading process, setting them transparent.
// Setting transparent
tableView.separatorColor = [UIColor clearColor];
And when you remove the loading UIView, you change the color for the one that you want.
// Setting the desired color
tableView.separatorColor = [UIColor grayColor];
Another alternative is to set the table view hidden while you don't have data to be displayed, and than when the first or more row appear you place you set the table view to not hidden.
I hope this will help you!
Cheers,
VFN
The problem was that self.view in the viewController had the tableView assigned directly. The solution is to assign the self.view a UIView and add the tableView and the custom UIView on top of self.view.
This is sample code from the viewController.
- (void)loadView {
UIView *aView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//tableView
self.tableView = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain] autorelease];
//set the rowHeight once for performance reasons.
//self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight = 75;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.autoresizesSubviews = YES;
[aView addSubview:tableView];
//set the rowHeight once for performance reasons.
self.view = aView;
[aView release];
}
- (void)viewDidLoad {
[super viewDidLoad];
loadingView = [[LoadingView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view addSubview:loadingView];
//fetch productsArray
[self productListRequestStart];
}