Make TableView background not scrollable - swift

today I've an other problem: I add a background image to a table view and I like that stay stationary when the cells scroll. I found this
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"background.png"]];
here iPhone Fixed-Position UITableView Background.
but I'm not able to "translate" from Objective-c to Swift... so can anyone help me?

You could try something like this.
var view = UIImageView(frame: /*whatever you want your frame to be*/)
view.image = //whatever you want your image to be
tableView.backgroundView = view

Related

How to add custom image to a UITableviewController in Xcode?

I am new to Xcode, and I am currently building my first app. I've searched long and hard to try and find the proper tutorial but I can't find one. I am looking for a way to be able to insert a custom background in my UITableViewController. I can change the color, but that is it. What I am looking to do is set my PNG image behind the static cells I've created, and drop the opacity on those cells so the custom image comes through. Can someone please give me a way to do this either through the IB (storyboard) or through the coding. Much appreciated!! Thanks in advance!
I think the easier and correct way is to:
[tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]]];
You can include this code in viewDidLoad to make it work.
for cell's background :, do this in willDisplayCell:atIndexPath: method
UIImageView *cellImageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:#"image.png"]]];
CGRect Frame = cell.backgroundView.bounds;
cellImageView.frame = newFrame;
[cell.backgroundView addSubview:cellImageView];
for tableview background , do this in viewDidLoad: method
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BackgroundPattern.png"]];
UIImageView *ivwCellBackground=[[UIImageView alloc]init];
[ivwCellBackground setImage:[UIImage imageNamed:CELL_BACKGROUNGIMAGE]];
cell.backgroundColor=[UIColor clearColor];
[cell setBackgroundView:ivwCellBackground];
[ivwCellBackground release];
ivwCellBackground=nil;

Twitters iPhone NavBar Logo

This is more like a design question but does anyone know how they made the Logo in the UINavigationBar look that good in the native iPhone app?
Does anyone know a tutorial?
Thanks in advance!
A UIViewController's -navigationItem property has itself a property called -titleView.
Set that titleView property of the currently displayed UIViewController's navigationItem to an embossed, semi-transparent icon like Twitter has done, and you're set.
You will load an image, and then put it in the navigationItem.titleView property on the view controller that will be inside the navigation controller.
UIImage *image = [UIImage imageNamed: #"header_logo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
viewController.navigationItem.titleView = imageView;
Basically both answers were correct.
I'm just typing the code I've used. I like it better than #stevenhepting 's code :)
Just put this code in the viewDidLoad: method
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourPicture"]];
The perfect size for your image (in my opinion) is 80x40 pixels (width=80 / height=40).

Is it possible to change tableView.backgroundView frame?

I want to put my table view background below the nav bar. When I try to change frame nothing happens..
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, -100, 320, 480)];
imageView.image = self.theme.bgImage;
[self.tableView setBackgroundView:imageView];
In the docs it says:
A table view’s background view is automatically resized to match the
size of the table view.
So I think it's alway resized again by the framework.
Add UIImageview as subview to view and set appropriate image.Place imageview behind tableview. Make tableview background nil. Then try to change frame. This may help you.

Changing UITableViewCell width to fit a UIImageView outside of the cell

I would like to know the best way/correct way to achieve from following layout? I want to place an UIImageView outside of UITableViewCell in a UITableView with static cells.
I have done the following by subclassing UITableViewCell for the cells in section 1 using the following code
- (void)setFrame:(CGRect)frame {
frame.size.width -= 125.0;
[super setFrame:frame];
}
In the UITableViewController's viewDidLoad I add the UIImageView and position it according to the width of the custom UITableViewCell.
This sort of works, but i'm not sure how to deal with rotation and also if what i've done so far would be the correct way?
there are differnt ways to do it. one is to set the width of table view less as you showd in pic 2nd is to use custom table view cell and on required cell add image so that your cell data as well as image will be shown. i think custom cell would be the better solution. tell me if you are asking the same thing what i answered, if no, then i review my answer thank.
I managed to produce what I wanted using the follow, this is proberly not the best way or cleanest way but as no one from StackOverFlow gave any better suggestions I thought I better answer this.
I subclassed the first 3 UITableViewCells and set a frame size to take into account the size of my image.
- (void)setFrame:(CGRect)frame {
float cellWidth = (frame.size.width - 345);
frame.size.width = cellWidth;
[super setFrame:frame];
}
Then in my UITableViewController's viewDidLoad I create and position the UIImageView using the first static cell in the tableview as an IBOutlet ("firstCell"). I then set the autoResizingMask which sorts out rotation and finally add the UIImageView to the view.
//Create and position the image view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((firstCell.frame.size.width), (firstCell.frame.origin.y + 70), 300, 137)];
// Add border and round corners of image view to make style look a little like tableviewcells
[imageView.layer setBorderColor:[UIColor colorWithWhite:0 alpha:0.5].CGColor];
[imageView.layer setBorderWidth:2.0];
[imageView.layer setCornerRadius:5.0];
[imageView setClipsToBounds:YES];
//Set the image in the image view
UIImage *image = [UIImage imageNamed:#"defaultPicture.png"];
imageView.image = image;
//Set resizing of image view for when view is rotated
imageView.contentMode = UIViewContentModeRedraw;
imageView.autoresizingMask = UIViewContentModeScaleAspectFit;
//Add tap gesture for imageview to initiate taking picture.
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *imageViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(takePicture:)];
[imageView addGestureRecognizer:imageViewTap];
//Add image view to view
[self.view addSubview:imageView];
This has not been fully tested and i'm sure isn't a great implementation, but its a starting point for the effect i'm after. Please reply if you know of a better way.
Note: The above code is written for my app on the iPad but screenshots are from testing I did on iPhone

How to set the background of a UITableView to an image?

I am using a UITableViewController which uploads a table. I have a Nib File with UITableView in it.Now I want to set the background of the tableView either from interface builder or from the TableViewController to an image.
How to do that.
OK so I created an UIImage in my controller. Now when I add where do I need to add it.
When I try adding it and set the color of tableView to clearColor, it just shows me the Image and not the table although I make sure that image is being sent to back of all views.
Guys Please note that I am not dealing a UIView and adding a tableView as its subview But I am dealing with a UITableView .
Place a UIImageView behind the UITableView, then do this:
tableView.backgroundColor = [UIColor clearColor];
Somehow playing around I was able to find out a way.
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"parentViewBackground.png"]];
the image size should be
2x - 640*960
1x - 320*480
the size smaller than above, it will be tiled.
The backgroundColor = [UIColor colorWithPatternImage: image] method rkbang outlines works great. Here is another method to achieve the same effect by adding a new view to the parentViewController. This would be useful if you want to mask other contents the parentViewController might have.
In your UITableViewController subclass, in -viewDidLoad insert:
//make a cool background image
self.tableView.backgroundColor = [UIColor clearColor];
UIImage *patternImage = [UIImage imageNamed:#"backgroundImage.png"];
UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:patternImage];
[self.parentViewController.view addSubview:backgroundImageView];
[self.parentViewController.view sendSubviewToBack:backgroundImageView];
//release the background image and image view (the backgroundImageView is still retained by the parentViewController)
[patternImage release];
[backgroundImageView release];
You may wish to keep track of the backgroundImageView so you can remove it or replace it. The code example above leaves it to the parentViewController to manage the new view. If you're loading and unloading this UITableView, you'll be leaking these UIImageViews with every load unless the parentViewController is releasing them at the same rate somehow.
What you can do is set the backgroundColor property of the tableview to [UIColor clearColor] and have a UIImageView of the same size and position underneath your TableView to show your background.
TableView properties include background color. Set this to clearColor; and put the image behind it. (In IB I think you'll have to delete the tableview add an image and then add your tableview back again)
if you're subclassing UITableViewController you get a tableview for free, no ivar or nib required, however since you're using a background I would stick with IB.
You can use the table-view's backgroundView. This worked for me.
[self.tableView setBackgroundView: [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"????.png"]]];
where ????.png is your background image.