How to place UILabel in the center of navigation bar - ios

How to place UILabel in the center of navigation bar in XCode 6? Is it possible at all? I can place here, for example, UIButton, but unable to place UILabel. If no, what can I do then? Place a UIButton with the appropriate text and make it non-clickable?
Thanks in advance.

In Xcode 6 if you want to put a label inside the UINavigationBar firstly you have to put a UIView there then put the UILabel inside the UIView (this is from the Storyboard btw).
If you do not put the UIView first then the UILabel will never get put onto the UINavigationBar.

Create a UIView and add UILabel as it's subview and then set your NavigationItem's titleView as previously created UIView.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
label.text = #"Hello";
label.textAlignment = NSTextAlignmentCenter;
[view addSubview:label];
self.navigationItem.titleView = view;
Note: Don't forget to set your own frame values to get better result.

Just make sure you try viewWillLayoutSubviews method instead of viewDidLoad that was what worked for me. Cheers!

Related

alignment in textfield does not work

I want both my placeholder and typed text to be aligned in the center.
I tried both of these options for the two textfields I have, but still the placeholder and whatever I type are aligned left.
self.teamA_name.textAlignment=NSTextAlignmentCenter;
self.teamB_name.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
What am I missing?
Textfields are created through IB.
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,15, 20)];
_txtCityName.leftView = paddingView;
_txtCityName.leftViewMode = UITextFieldViewModeAlways;
Use this code to set inset..This works for me
Currently I'm setting padding view width as 15...change this as your requirement and make it in the center

How to shove a label in a tableview full of buttons?

I have a table view full of buttons. A label is outside the tableview serving as heading. I want to shove it inside the table view, on top of course, without changing it to button. In other words I want to move it inside the tableview, but on top and not as a button,as a heading.
The label in question says "Select all things that apply to your home"
For code please see Set title labels of buttons in table view from an array full of strings
For Screenshot pls visit http://i60.tinypic.com/2qmf4wl.png
How about this?
UIView *headerView;
UILabel *labelView;
labelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
labelView.font = [UIFont fontWithName:#"Trebuchet MS" size:22.0];
labelView.backgroundColor = [UIColor clearColor];
labelView.text = #"* Your important note here.";
labelView.textColor = [UIColor blackColor];
[headerView addSubview:labelView];
yourTableView.tableHeaderView = headerView;
It's hard to add a label to a tableviewcontroller. Two options I can think of:
Make "Select all things that apply to your home" your navigation item title. (you can do this right in the storyboard)
Place a table view in a normal view controller and leave enough space on top for a label. (again, you can drag a normal table view into a view controller in a storyboard)
Hope it helps!
you can creat a subclass of UITableViewCell and custom the cell for the tabe

Remove back button text and center title in navigation bar

I've removed the text for UIBarButton in AppDelegate:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -1000.f) forBarMetrics:UIBarMetricsDefault];
Which works like a charm:
As you can see, this doesn't align the navigation title at horizontal center. What is the best solution to accomplish this globally for all views.
PS: I am using Storyboard.
You can create your own custom titleView with a UILabel as follows:
UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0,0,100,50)];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,100,50)];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = #"PAKKELISTE";
[titleView addSubview:titleLabel];
self.navigationItem.titleView = titleView;
The details of the frames, text, alignment, etc. are just an example. The main idea though is that you set a custom UIView as the navigationItem's titleView.
It could also be an issue with your back button offset. Try this approach instead for removing the "back" text (I haven't tried this before, but I'm curious if it will work).
self.navigationController.navigationBar.topItem.title = #"";

2 UIBarButtonItem's using the same custom view only added once

I want to add a thin line between items in my UIToolBar so I'm creating a UIBarButtonItem with a custom view like so:
UILabel *separatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 44)];
separatorLabel.backgroundColor = [UIColor colorWithRGB:0xe5edec];
UIBarButtonItem *separator = [[UIBarButtonItem alloc] initWithCustomView:separatorLabel];
Then I add my separator to the items array:
[items addObjectsFromArray:[NSMutableArray arrayWithObjects:someButton1, separator, somebutton2, separator, someButton3, nil]];
I thought this would add 2 separators to my toolbar but it only adds the one at the end. Why is this?
I can fix this by creating 2 duplicate labels and UIBarButtonItem's, but is there any other better way?
Any given instance of UIView can only appear in once in the view hierarchy. If you think about the APIs defined on UIView, this is fairly obvious. For example, UIView has a frame property which defines it's location in the superview. The frame property wouldn't make sense if the viewed appeared in two places.
So you need multiple instances. You can streamline your code by defining a method that creates separators:
- (UILabel *)newSeparator
{
UILabel *separatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 44)];
separatorLabel.backgroundColor = [UIColor colorWithRGB:0xe5edec];
UIBarButtonItem *separator = [[UIBarButtonItem alloc] initWithCustomView:separatorLabel];
return separator;
}
And then you can add your items like this:
[items addObjectsFromArray:#[button1, [self newSeparator], button2, [self newSeparator]];
Also, you don't need to use UILabel if you're only displaying a background color. You can just use UIView.
Yes,you just created only one UIBarButtonItem object,so it showed one.
I think the better way is creating a UIBarButtonItem subclass with custom label,then create two objects of the subclass.
I hope my answer can help you.

How do I put images and text in the same layout in XCode

this is my first post. How can I add text and images to an app. i am only making an image, text and button based app and have made it for android as the picture shows below. I am now trying to do this in iOS but i am unable to place image after text followed by text again.
In android I was able to put multiple ImageView and TextViews inside a scroll layout, but I am unable to do this in storyboard Xcode 4.5/4.6.
If you could give me simple ideas to fix reproduce the same style of layouts in Xcode for iPhone and iPad, it would be great. I am not familiar with Xcode, so if it is possible to solve this easily, could you please help.
I want to be able to make a long scrollable page with many text line and images as you may have already understood from the image below.
Thanks in advance
Sorry was unable to post image. may try again or post website link.
You need to create a UIScrollView -> init UIScrollView with frame and add it to your app, set the content size of your scroll view.
Create the UILabel for the text. -> init UILabel with frame and text, add to your scroll view as a subview.
Create the UIImageView for the image -> init UIImageView with frame and image, add to your scroll view as a subview.
Typically, that is all if you do it in the way programmatically.
some code (without test):
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 100)];
label1.text = #"label1";
[scrollView addSubview:label1];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 300, 500)];
imageView.image = [UIImage imageNamed:#"imagename.png"];
[scrollView addSubview:imageView];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, label1.frame.size.height+imageView.frame.size.height);
[self.view addSubview:scrollView];
ps: If you want do it with nib, I could find some capture for you.

Resources