Creating Views without IB - objective-c

It's pretty embarrassing asking this because I've been building apps for a while now. In the past I've always used Interface Builder to set up my views etc.
Here is my loadView:
- (void)loadView {
UIView *splashView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
splashView.backgroundColor = [UIColor clearColor];
UIImageView *splashImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[[NSBundle mainBundle]pathForResource:#"splash" ofType:#"jpg"]]];
splashImage.frame = [splashView bounds];
[splashView addSubview:splashImage];
self.view = splashView;
}
For some reason the imageView isn't appearing. No errors, but just no appearance. The UIView is added, but the UIImageView isn't.
Like I said, I've traditionally done this with IB so I'm not so familiar with the programatic method.
Any help appreciated.

Add splashView to self.view:
self.view = splashView;
or add your splashImage into self.view. In this case you don't need the UIView - splashView:
UIImageView *splashImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[[NSBundle mainBundle]pathForResource:#"splash" ofType:#"jpg"]]];
splashImage.frame = [splashView bounds];
[self.view addSubview:splashImage];

Try not creating a new view called splash view and just add the image view to your self.view.

Related

UIView does not show properly when the uitableview is scrolled down

I'm implementing some sort of loading screen consisting of a black coloured uiview with a spinner at it's center.
When the tableview has been scrolled down and the loading goes, the loading screen is at the top and if the tableview has been scrolled to the bottom, the loading screen disappears.
Function:
UIView *blackScreen = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
blackScreen.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
blackScreen.tag = LOADING_SCREEN_TAG;
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(CGRectGetWidth(self.view.bounds)/2,
CGRectGetHeight(self.view.bounds)/2.3);
spinner.tag = SPINNER_TAG;
[spinner startAnimating];
[self.view addSubview:blackScreen];
[self.view addSubview:spinner];
[self.view setUserInteractionEnabled:NO];
You need to add contentOffset, I would like to refer you to this page which explains the use of it.
Here's the solution anyway, this is assuming that the variable name of your tableview is tableView and is set as a property of the class:
CGRect loadingScreenFrame = CGRectMake([[UIScreen mainScreen] bounds].origin.x + self.tableView.contentOffset.x,
[[UIScreen mainScreen] bounds].origin.y + self.tableView.contentOffset.y,
[[UIScreen mainScreen] bounds].size.width,
[[UIScreen mainScreen] bounds].size.height);
UIView *blackScreen = [[UIView alloc] initWithFrame:loadingScreenFrame];
blackScreen.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
blackScreen.tag = LOADING_SCREEN_TAG;
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake((CGRectGetWidth(self.view.bounds)/2) + self.tableView.contentOffset.x,
(CGRectGetHeight(self.view.bounds)/2.3) + self.tableView.contentOffset.y);
spinner.tag = SPINNER_TAG;
[spinner startAnimating];
[self.view addSubview:blackScreen];
[self.view addSubview:spinner];
[self.view setUserInteractionEnabled:NO];
A solution would be to not add the spinning view to the tableview but rather to its superview. It seems like you are probably using a UITableViewController which would mean that you would need to restructure your code to use a UIViewController to make this work. Adding both the UITableView and the spinner view to the view of the UIViewController would also achieve what you are looking for without having to set the center based on the content offset.

Add logo to UINavigationBar

I'm adding the following code to my app delegate to get my logo to the UINavigationBar, but it is not working. I have the Logo.png and Logo#2x.png inside the project.
What am I doing wrong?
UIImageView *titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Logo.png"]];
[[UINavigationBar appearance] setTitleView:titleView];
Yes, iOS is case-sensitive. Try it again but change:
UIImageView *titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Logo.png"]];
to
UIImageView *titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
If I understand your question correctly, then what you'll want to do is to set the tileImage in your ViewController's navigation item like so:
- (void)viewDidLoad
{
[super viewDidLoad];
// Set up the nav bar
UIImage *titleImage = [UIImage imageNamed:#"Logo.png"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:titleImage];
}

how to make a imageview do a segue

I have a bunch of imageview generated in a gridview. But now I want that each imageview can do a segue to another screen. Does anybody knows how I can do that? Here is the code of how I generated my imageview and put it into my grid.
UIImageView * myView = [[UIImageView alloc] initWithImage:myImage];
CGRect rect = myView.frame;
rect.origin.x = xCurrent;
rect.origin.y = yCurrent;
myView.frame = rect;
myView.tag = cellCounter;
[gridContainerView addSubview:myView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
myView.userInteractionEnabled = YES;
[myView addGestureRecognizer:tap];
Please help
Kind regards
You are looking for a tapGestureRecognizer. Add one of those to your image views and make the action call your segue method.
UIImageView *img = [[UIImageView alloc] init];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(mySequeMethod:)];
img.userInteractionEnabled = YES;
[img addGestureRecognizer:tap];
A:
use a custom button instead of UIImageView, and set the image... and then segue with that button, (it looks the same)
B:
use a gesture recognizer to the view, and use a tap with one finger.

Nothing happens when addSubview image

Here is the code:
CGRect myImageRect = CGRectMake(0, 0, 100, 100);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"Angry0.png"]];
[mainView addSubview:myImage];
Angry0.png is in my Resources folder. No image is display on simulator.
Right... You need to initalize all your views...
If I'm right your view hierarchy is this:
-mainView
-contentView
-scrollView
-imageView
So somewhere when you load mainView you also need to initalize all the child views...
For example:
...
//put this where you call mainView = [[UIView alloc] init...
contentView = [[UIView alloc] init...
//config the contentView
scrollView = [[UIScrollView alloc] init...
//config the scrollView
....
Now, providing that you correctly set your views frames and initialized them they shouldn't be null...
You can check by calling:
if (!scrollView) {
NSLog(#"scrollView is null :(");
}
Then you should be able to call your existing code and all should be good

UIWebView on iPad size

I am using the UIWebView on iPad, which I initialize by the code below. The problem is that the view is never displayed on the top of the page just under the Status bar, but there is another 44px space (filled with black color) - for Navigation Bar, which I do not want to display. Any hints how I can make the UIWebView be displayed without the 44px space?
Thanks a lot,
BR
STeN
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect rectApp = [[UIScreen mainScreen] applicationFrame];
self.webView = [[[UIWebView alloc] initWithFrame:rectApp] autorelease];
self.webView.backgroundColor = [UIColor whiteColor];
self.webView.scalesPageToFit = YES;
self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.webView.delegate = self;
[self.view addSubview: self.webView];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.test.com/shop/index.php"]]];
}
The problem is that the coordinate system of the view you're adding it to isn't the coordinate system of the window; the view has already been adjusted for the status bar.
Change it thus:
CGRect rectApp = [[UIScreen mainScreen] applicationFrame];
rectApp.origin = CGPointZero;
Or better yet, use self.view.bounds, since self.view presumably refers to a view that fills the application's window anyway.
I like Tony's answer. When I ran into this problem I used the generic view.frame = self.view.bounds, in your code this would be written:
self.webView.frame = self.view.bounds;