Add a label to UINavigationBar in addition to the titleView - xcode

I'm using a UINavigationController to push my views. I want to add a UILabel above the title in the UINavigationBar of the root view controller.
When I use the following code, the label appears, but under the UINavigationBar:
[self.view addSubview:newLabel];
Is there a way to add the label to the UINavigationBar or to the UIView so that it appears above the navigation bar?
I know I can create a new UINavigationBar and build it with multiple controls like this:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navBar.barStyle = UIBarStyleBlack;
navBar.translucent = YES;
[self.view addSubview:navBar];
[navBar release];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.text = #"My Text";
[navBar addSubview:navLabel];
[navLabel release];
However, doesn't that defeat the purpose of using the navigation controller?

You can refer this site, may be it help you in sorting your problem
http://iphonesdevsdk.blogspot.com/2011/04/uinavigationbar.html

Related

How do I find the reference/identifier of a button on the storyboard in xcode?

Thanks for reading; this is my problem, I'm trying to hook up code I have written to buttons which already exist in the storyboard but I can't find a reference or identifier for the button(s).
I want to be able to modify buttons which have been created on the storyboard and style/give actions in my code.
So I would like something I can reference like so:
UIButton *subBtn;
where subBtn is the button's reference/identifier.
tldr; where do you find the reference for buttons in xcode (v6+) storyboard?
For those who come after me:
UIButton *subBtn = (UIButton *) [self.view viewWithTag:9];
The Line above allows you to reference a button with an "tag" which you can find in the attributes tab in the storyboard.
By using this it allows you to change attributes like below, to format your buttons:
[subBtn addTarget:self action:#selector(submitHit) forControlEvents:UIControlEventTouchUpInside];
UIImage *UISubImg = [UIImage imageNamed: #"submit_button.png"];
UIImage *UISubImg2 = [UIImage imageNamed: #"submit-play-icon.png"];
[subBtn setTitle:#"SUBMIT" forState:UIControlStateNormal];
subBtn.frame = CGRectMake(20, 400, 280, 65);
[self.view addSubview:subBtn];
[subBtn setBackgroundImage:UISubImg forState:UIControlStateNormal];
[subBtn setImage:UISubImg2 forState:UIControlStateNormal];
SubmitHit is a method.

Container view popover view programmatically

In iPad View I add Container View, after adding Xcode automatically create new view and connect to my main view with embed segue. Now in code I try to call this new view programmatically and cannot do this, I need to show this form with popover.
I try:
PaymentCashController *newPopup = [self.storyboard instantiateViewControllerWithIdentifier:#"PaymentCash"];
[self presentViewController:newPopup animated:YES completion:nil];
This work but show new view in full screen but I need show with popover. I try many things and all not working (.
I try this:
[self performSegueWithIdentifier:#"PaymentCash" sender:nil];
But receive exception SIGABRT
I try this:
PaymentCashController *newPopup = [self.storyboard instantiateViewControllerWithIdentifier:#"PaymentCash"];
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:newPopup];
popoverController.popoverContentSize = CGSizeMake(500, 400);
CGRect popoverStartFrame = {50, 500, 100, 100};//Set the frame that you would like your popover to present from.
[popoverController presentPopoverFromRect: popoverStartFrame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];
Receive exception SIGABRT
Try this:
PaymentCashController *newPopup = [PaymentCashController new];
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:newPopup];
popoverController = [[UIPopoverController alloc] initWithContentViewController:newPopup];
[popoverController presentPopoverFromRect:CGRectMake(44, 6, 111, 111) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
Same SIGABRT
You need to create UIPopoverController with your view controller and present the popover as shown below.
PaymentCashController *newPopup = [self.storyboard instantiateViewControllerWithIdentifier:#"PaymentCash"];
UIPopoverController *popoverController = [[UIPopoverController alloc] newPopup];
popover.delegate = self;
//set custom size you want for popover
popover.popoverContentSize = CGSizeMake(500, 400);
CGRect popoverStartFrame = {50, 500, 100, 100};//Set the frame that you would like your popover to present from.
[popover presentPopoverFromRect: popoverStartFrame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];
Many experiments and now all working:
Remove all segue to my popover View in Storyboard. Do it freeform and change size.
Add to my main View new property
#property (nonatomic,strong) UIPopoverController *popOver;
Set storyboard id to my popover View to "PaymentCash".
In any action on my View to show my popover View add this code:
PaymentCashController *PopoverView = [self.storyboard instantiateViewControllerWithIdentifier:#"PaymentCash"];
self.popOver =[[UIPopoverController alloc] initWithContentViewController:PopoverView];
CGRect popoverStartFrame = {1,500,200,1};
[self.popOver presentPopoverFromRect:popoverStartFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
Only one problem after this, popover View show in no correct place but this another question.

Setting the height of the UINavigationBar

I have set a UINavigationBar background image to an image with a height of 68px as opposed to the default 44px. Now the image is shown fine, but the content in the UINavigationController is overlapped in the first (68-44)px = 12px by the header image.
How do I fix this? I have tried setting the frame of the UINavigationBar without luck.
Thanks, Caspar.
This is my code (AppDelegate.m):
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.frontTableViewController];
UINavigationBar *navBar = navigationController.navigationBar;
[navBar setBackgroundImage:[UIImage imageNamed:#"header"] forBarMetrics:UIBarMetricsDefault];
self.window.rootViewController = navigationController;
try setting the image for the navigationbar this way
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myNavBarImage.png"]];
or have a look at this Answer

Xcode - UILabel is not changing value on viewDidAppear

Hey everyone, I have a iPhone App I am creating. It uses a uitableview and that goes to a detail view.
Now on the detail view I have a scroll view, and all the data (XML) comes into it fine, each time, no issues.
My UILabels in my scrollview do update with the correct data, but, they keep adding subviews on top of each other to keep adding label after label and it all looks mumbo jumbo.
I have tried everything to try and remove the subviews such as:
for(UIView *subview in [scrollView subviews]) {
[subview removeFromSuperview];
}
AND
[[scrollView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
So I am stumped, I got no idea what is happening. Any help? I have been searching Google for ages and agesm, but keep finding the same answers that don't work :(
Any direction to sites that might help that you know of will be greatly appreciated.
Thanks!
Here is what I have in my controller that loads all the scrollview and labels:
- (void)viewDidAppear:(BOOL)animated {
// Create the scroll view for this view
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(320, 270);
[scrollView setFrame:CGRectMake(0, 198, 320, 170)];
// Do the labels for the text
UILabel *dates = [[UILabel alloc] initWithFrame:scrollView.bounds];
dates.textColor = [UIColor whiteColor];
dates.font = [UIFont boldSystemFontOfSize:12];
dates.text = round_date;
dates.tag = 1;
[dates setBackgroundColor:[UIColor clearColor]];
[dates setFrame:CGRectMake(10, 10, 280, 22)];
[scrollView addSubview:dates];
[dates release];
// Add the content to the main scrollview
[self.view addSubview:scrollView];
[scrollView release];
}
Ok, the reason for this is that viewDidAppear will get called every time you present the view.
If you do this is
-(void) viewDidLoad;
instead.
To make sure you can access them later you might want to keep a reference to them in your UIViewController
-(void) viewDidLoad {
scrollView = [[UIScrollView alloc] initWithFrame];
[self.view addSubview:scrollView];
}
and define it in your header file with:
UIScrollView *scrollView;

UIScrollView viewForZoomingInScrollView when pagingEnabled with multiple UIImageViews in it

I have this UIScrollView with pagingEnabled to let the user page between some images.
Now I want to let the user zoom on each one of the images. How do I do that? What I have now zooms on my UIScrollView from its origin and I need it to zoom on each picture, not the hole UIScrollView.
What I have now:
UIScrollView* containerView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
containerView.scrollEnabled = YES;
containerView.pagingEnabled = YES;
[containerView setMaximumZoomScale:2.0];
[containerView setDelegate:self];
self.view = containerView;
UIImage *imageOne = [UIImage imageNamed:image01];
UIImageView *viewOne = [[UIImageView alloc] initWithImage:imageOne];
viewOne.frame = CGRectMake(0, 44, 320, 480);
[containerView addSubview:viewOne];
UIImage *imageTwo = [UIImage imageNamed:image02];
UIImageView *viewTwo = [[UIImageView alloc] initWithImage:imageTwo];
viewTwo.frame = CGRectMake(320, 44, 320, 480);
[containerView addSubview:viewTwo];
UIImage *imageThree = [UIImage imageNamed:image03];
UIImageView *viewThree = [[UIImageView alloc] initWithImage:imageThree];
viewThree.frame = CGRectMake(640, 44, 320, 480);
[containerView addSubview:viewThree];
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)containerView
{
return containerView;
}
Thanks in advance
You have to return a view contained in your scrool. As you have 3 UIImage, try with this...
In .h
UIView *auxView;
In .m
...
[auxView addSubview:imageOne];
[auxView addSubview:imageTwo];
[auxView addSubview:imageThree];
[containerView addSubview:auxView];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)containerView{
return auxView; }
I hope this help you
I think you mean you want to zoom each UIImageView individually, right?
I was struggling with this problem many weeks, then I decide to wrote a class to solve this problem.
Here is my github repo of this class: https://github.com/windmemory/PhotoCollectionView
My solution to this problem is to create single UIScrollView to contain every UIImageView, and then add these UIScrollView to the containerView.
If we add all the UIImageView to a single view then make it zoomable, when we zoom one photo, all these photos will be zoomed, then the contentSize will change accordingly. Once the contentSize is changed, it will messed up all the paging thing. So the best way I came up is to use UIScrollView to contain UIImageView, then put every UIScrollView into the containerView, this will allow you to zoom each photo separately, which won't mess up all the paging thing you set up.
Hope this may help you.

Resources