UIScrollView and UINavigationController not working with iOS 4 - ios

i have a problem with UIScrollView and UINavigationController(s) and iOS 4. The following code works fine with iOS 5:
////////////////////////////////////////////////////////////////////////////////////
/* Scroll View */
////////////////////////////////////////////////////////////////////////////////////
mScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 88, 1024, 668)];
mScrollView.contentSize = CGSizeMake(1850, 668);
////////////////////////////////////////////////////////////////////////////////////
// First View
////////////////////////////////////////////////////////////////////////////////////
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"view_bg.png"]];
imgView.frame= CGRectMake(10, 50, 350, 510);
[mScrollView addSubview:imgView];
[imgView release];
mFirstView = [[[FirstView alloc] init] autorelease];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mFirstView];
navigationController.navigationBar.tintColor = [UIColor colorWithRed:79.0/255 green:143.0/255 blue:0.0/255 alpha:1];
navigationController.view.frame = CGRectMake(25, 65, 320, 480);
[mScrollView addSubview:navigationController.view];
////////////////////////////////////////////////////////////////////////////////////
// Second View
////////////////////////////////////////////////////////////////////////////////////
UIImageView *imgView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"view_bg.png"]];
imgView2.frame= CGRectMake(380, 50, 350, 510);
[mScrollView addSubview:imgView2];
[imgView2 release];
mSecondView = [[[SecondView alloc] init] autorelease];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:mSecondView];
navigationController2.navigationBar.tintColor = [UIColor colorWithRed:79.0/255 green:143.0/255 blue:0.0/255 alpha:1];
navigationController2.view.frame = CGRectMake(395, 65, 320, 480);
[mScrollView addSubview:navigationController2.view];
// finally add the scroll view to the parent view
[self.view addSubview:mScrollView];
All i do is, to add a ScrollView with 2 NavigationControllers (with a allocated root view controller) do the view. But using this code with iOS 4 does not show the right size of the ViewControllers added. The ViewControllers are the size of the whole iPad screen!!!!!
As i already said, it works on iOS 5! Even when i change the size of the views added manually it is not working, the added view always fills the whole iPad screen!! Any ideas to fix this?
Thank you in advance!

in your code you have the following
//first view you add mfirstview.view
[mScrollView addSubview:mFirstView.view];
//second view you add navigationController2.view
[mScrollView addSubview: navigationController2.view];
So i guess you have to change the first view code to do the following
[mScrollView addSubview: navigationController.view];

Related

Resize view to account for toolbar

If I create a basic ViewController and add an opaque UIToolbar to the view, like so:
UIViewController* viewController = [[UIViewController alloc] init];
[viewController.view setBackgroundColor:[UIColor blueColor]]; // To make it easy to see
self.window.rootViewController = viewController;
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, viewController.view.frame.size.height - 49, viewController.view.frame.size.width, 49)]; // At bottom. Height of 49
[toolbar setBarStyle:UIBarStyleBlack]; // Opaque and easy to see
[self.window.rootViewController.view addSubview:toolbar]; // Add toolbar to view controller
The view hierarchy ends up like below:
How do I make the View controller resize it's main area (the blue part) so it doesn't extend behind the UIToolbar?
Try to add this line of code
viewController.edgesForExtendedLayout = UIRectEdgeNone;
this will make so view controller's edges are not extended in any direction.
Or you can add this if you want to extend top, left and right, but not bottom.
viewController.edgesForExtendedLayout = UIRectEdgeTop, UIRectEdgeLeft, UIRectEdgeRight;
Try like this
UIViewController* viewController = [[UIViewController alloc] init];
[viewController.view setBackgroundColor:[UIColor blueColor]]; // To make it easy to see
self.window.rootViewController = viewController;
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, viewController.view.frame.size.height - 49, viewController.view.frame.size.width, 49)]; // At bottom. Height of 49
[toolbar setBarStyle:UIBarStyleBlack]; // Opaque and easy to see
SecondViewController* secondChildVC = [self.storyboard instantiateViewControllerWithIdentifier:#"someId"];
[secondChildVC.view setBackgroundColor:[UIColor redColor]];
[viewController addChildViewController:secondChildVC];
[secondChildVC didMoveToParentViewController:viewController];
secondChildVC.view.frame = CGRectMake(0,0,viewController.view.frame.size.width,viewController.view.frame.size.height - 49);
[viewController.view addSubview:secondChildVC.view];
[self.window.rootViewController.view addSubview:toolbar];

How to add a custom UIView to Navigation Bar in iOS7+ like this

Is there a way to insert any UIView in NavigationBar like the image below?
Is this not working?
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
[[self view] addSubview: tempView];
[[self view] addSubview: navBar];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: #"Controls"];
[navBar pushNavigationItem:navItem animated:NO];
In the code below, I embed a view into the navbar, where my embedded view draws the background and the new buttons. The code also deals with relaying out the view when the device is rotated.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (view.superview == nil) {
[self.navigationBar addSubview:view];
[self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
}
CGRect frame = self.navigationBar.frame;
frame.size.height = MY_NAVBAR_HEIGHT;
self.navigationBar.frame = frame;
view.frame = self.navigationBar.bounds;
}
An alternative solution is to use a regular UIView as a NavBar, and by saying that what I mean is you have to set the UIView in navbar place and hide the NavigationController bar in the main VC and show it in the next one read this article Here https://medium.com/me/stats/post/54bc5fc56d6c

No UIView appearing in the interface

NOTE: Jump to end for answer.
I am new to IOS Programming. How can I add a visible UIView to the following code. So far it just has the nav bar appear, and black space below, no imageView or the square UIView in the code below.
Below is code in my HomeScreenViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setTintColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
Below is code in AppDelegate.
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.myViewController = [[HomeScreenViewController alloc] init];
UINavigationController *navBar = [[UINavigationController alloc] initWithRootViewController:self.myViewController];
navBar.title = #"Navigation";
self.window.rootViewController = navBar;
[self.window makeKeyAndVisible];
return YES;
}
EDIT:
Here is also the interface for appDelegate. This is literally the only code in the proj other than the generated lines upon creating the project in xcode. I also deleted the storyboard file along with its info in the plist file (otherwise the app would crash).
#import <UIKit/UIKit.h>
#import "HomeScreenViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, strong) HomeScreenViewController *myViewController;
#end
EDITAgain:
No Need to Read Comments, just read this:
Yes, viewDidLoad does invoke.
The background changes to red if i set background color.
No I am not using storyboard.
Still unanswered.
http://postimg.org/image/h5ayao2q3/
FINAL EDIT: Finally! problem solved. It was as simple as setting the background colour for the UIView square. D'oh.
- (void)loadView
{
[super loadView];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view setBackgroundColor:[UIColor redColor]];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
Thank you very much everyone for your help. I guess my UIImage is borked, but I can figure that out myself.
Try changing it:
self.myViewController = [[HomeScreenViewController alloc] init];
TO:
self.myViewController = [[HomeScreenViewController alloc] initWithNibName:#"HomeScreenViewController" bundle:nil];
And Make sure there are some label or some thing in HomeScreenViewController. Other part of your code looks fine. Hope this helps. :)
First put breakpoint and verify that ViewDidLoad method is getting called.
If not , since you mentioned that you are creating the views programmatically you need to override the - (void)loadView method and create the view for the view controller.
-(void)loadView {
self.view = [[UIView alloc] init];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
}
Changed the line [square setTintColor:[UIColor orangeColor]]; to [square setBackgroundColor:[UIColor orangeColor]];
right click your UIViewController and check view connection is established to the xib or storyboard view.
ViewDidload will not get called unless view loads from storyboard or xib.
loadView method gets called if you are creating UIView programatically
If you are not using story board or XIB then you need to add the following method
-(void)loadView{
//load the views
}
I have two options to solve this problem:
1) If you have XIB file corresponding to the controller:
HomeScreenViewController *homeScreenViewController = [[HomeScreenViewController alloc] initWithNibName:#"HomeScreenViewController"] bundle:nil];
1a. Verify the name of your XIB is correct.
1b. Goto XIB file of HomeScreenViewController -> File Owner -> Connection Inspector -> Check the view outlet is connected.
2) If you do it programmatically, add this to the end of your loadView function:
self.view = square;
so the final code should look like:
- (void)loadView
{
//[super loadView];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // <-- look at here, your view has changed its frame now
[self.view setBackgroundColor:[UIColor redColor]];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setTintColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}

how to show the views added in view controller

I had created the the view programatically but it does not display on the screen
and gives a message in the console that
Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps
the designated entry point is not set?
Here is my code
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
exmTableViewController *vc =[[exmTableViewController alloc] init];
self.window.rootViewController=vc;
self.window.backgroundColor =[UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
*Code In Viewdidload *
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
self.imageView=img;
[self.view addSubview:self.imageView];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
lbl.text =#"Hello";
self.view =lbl;
}
Instead this line self.view =lbl;, use [self.view addSubview:lbl]; to add subview to your view.
try this
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
self.imageView=img;
[self.view addSubview:self.imageView];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
lbl.text =#"Hello";
[self.view addSubview:lbl];
}
-(void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)]; [self.view addSubview:img];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 200, 100, 20)];
lbl.text =#"Hello";
[self.view addSubview:lbl];
}
Try like this. self.view = lbl; in this line you assign self.view is as label you want to add subview [self.view addSubview:yourSubView]; do like this.
When you are creating View controller from storyboard, view controller is initiated by storyboard. You should not create windows root controller by yourself. You need to provide story board name in Plist.
After loading view viewDidLoad method will be called. Here you need to set up view(Adding and configuring subviews). But you are changing viewControllers view property, that is set by storyboard. If you want to change your view controllers view you can use,
- (void)loadView
// inside this method you can assign new view to view property.
If you are adding view as subview use view's method
- (void)addSubview:(UIView *)view

ios background position

What is the best way to position background image below header image. Currently I have:
header = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"h.png"]];
[header setContentMode:UIViewContentModeTop];
[self.window addSubview:header];
background = [[UIImageView alloc] init];
[background setImage:[UIImage imageNamed:#"bg.png"]];
[background setFrameOrigin:CGPointMake(0,header.frameHeight)];
[background addSubview:background];
but I get the following error:
No visible #interface for 'UIImageView' declares the sector 'setFrameOrigin.'
I want to use UIView method, so that I can place a table view inside background view afterwards.
[background addSubview:background];
is not OK in your code. You are trying to add a view as subview to its self.
Just create a rootviewcontroller and set as in your app delegate as rootview controller.
self.window.rootViewController = rootviewcontroller;
then you can add your background view as subview on the view of rootviewcontroller. after that you can do it for headerview.
[rootviewcontroller.view addSubview:background];
[rootviewcontroller.view addSubview: headerview];
you can use:
CGRect rect = background.frame;
rect.oigin.x = x;
rect.origin.y = y;
background.frame = rect;
or:
background = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
[background setFrame:CGRectMake(0, header.frame.size.height, 60, 50)];
Try like this it will be helpful to you.

Resources