I use custom back btn in navigation controller:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(#"Back", nil)
style:UIBarButtonItemStylePlain
target:nil
action:nil];
//and add custom label in this:
heightNav = self.navigationController.navigationBar.frame.size.height;
widthNav = self.navigationController.navigationBar.frame.size.width;
self.aCNavLabel = [[ACNavLabel alloc] initWithFrame:CGRectMake(0, 0, widthNav, heightNav)];
self.aCNavLabel.text = NSLocalizedString(#"Settings", nil);
//
UIView *titleNavView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, widthNav, heightNav)];
self.navigationItem.titleView = titleNavView;
[self.navigationItem.titleView addSubview:self.aCNavLabel];
but label is not centered.
Please tell me where I made a mistake?
and custom label aCNavLabel:
#implementation ACNavLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.textColor = [UIColor blackColor];
self.font = FONT_OP_STD(22.0f);
// self.text = title;
self.textAlignment = NSTextAlignmentCenter;
}
return self;
}
I change some rules for this
self.navigationItem.titleView = self.aCNavLabel;
and and there was something incredible
this is first viewController and
and next what is this????
self.navigationController.navigationBar setTitleTextAttributes:(NSDictionary *)
This method will be Ok
you can customer the navigationBar title about the font,the color,the textAlign and other properties;
self.aCNavLabel = [[ACNavLabel alloc] initWithFrame:CGRectMake(0, 0, widthNav, heightNav)];
self.aCNavLabel.text = NSLocalizedString(#"Settings", nil);
self.aCNavLabel.textAlignment = NSTextAlignmentCenter; // SET THIS
You need to set the text alignment to center like:
aCNavLabel.textAlignment = NSTextAlignmentCenter;
Refer the UILabel textAlignment property for more options.
or as #NANNAV suggested use:
self.navigationItem.titleView = self.aCNavLabel;
aCNavLabel.textAlignment = NSTextAlignmentCenter;
try it.
Create category for UINavigationBar and programmatically adjust the position of your title label. set ACNavLabel view as titleview.
UINavigationBar+MyNavigationBar.h
#interface UINavigationBar (MyNavigationBar)
#end
UINavigationBar+MyNavigationBar.m
#implementation UINavigationBar (MYNavigationBar)
- (void)layoutSubviews {
for (UIView *view in self.subviews) {
if ([view isKindOfClass:[ACNavLabel class]]) {
view.center = CGPointMake(self.center.x, self.center.y);
}
}
}
#end
Related
i've been doing a thing here using objective-c, but i'm a beginner in this language, and that's what i did:
I've created two UIView class and named them as LabelView and ButtonView. What i wanted to do is when i touch the button the label text changes. I have given the code below which i did.
I get an error when i touch the button i've created.
Error: "-[LabelView setText:]: unrecognized selector sent to instance 0x7b66cf80"
Warning on the line: self.cslbl_ = lblView; -
"incompatible pointer types assigning to uilabel from label view".
- (void)viewDidLoad {
LabelView *lblView = [[LabelView alloc] init];
[lblView setFrame:CGRectMake(10, 100, 300, 50)];
//lblView.backgroundColor = [UIColor redColor];
self.cslbl_ = lblView;
[self.view addSubview:lblView];
ButtonView *btnView = [[ButtonView alloc] initWithFrame:CGRectMake(110, 200, 100, 50)];
SEL target = #selector(changeText:);
[btnView setSelector:self selector:target];
[self.view addSubview:btnView];
}
-(void) changeText:(UIButton *)btn {
static int k = 0;
if (k%2 == 0)
{
[self.cslbl_ setText:#"Apple is a healthy fruit"];
}
else
{
[self.cslbl_ setText:#"Apple"];
}
NSLog(#"Click value is %d",k);
k++;
}
Custom Label Class
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UILabel *myLbl = [[UILabel alloc] init];
myLbl.frame = CGRectMake(0, 0, 300, 50);
//myLbl.backgroundColor = [UIColor blueColor];
myLbl.text = #"Apple";
self.lbl_ = myLbl;
[self addSubview:myLbl];
}
return self;
}
Custom Button Class
-(id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UIButton *myBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[myBtn setTitle:#"Touch" forState:UIControlStateNormal];
[myBtn setTitle:#"Touched" forState:UIControlStateHighlighted];
myBtn.backgroundColor = [UIColor grayColor];
self.btn_ = myBtn;
[self addSubview:myBtn];
}
return self;
}
- (void)setSelector:(UIViewController *)vc selector:(SEL)changeText
{
[btn_ addTarget:vc action:changeText forControlEvents:UIControlEventTouchUpInside];
}
Though question is not clear but it seems that your self.cslbl_ is a UILabel and you are assigning lblView to it which of type UIView, thats the reason you are getting warning.
And you are trying to setText to LabelView which is again not a UILabel type that why you are getting crash.
First of all you LabelView should be a subclass of UILabel not UIView but still if its your requirement then you should use below code.
Custom Label Class
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UILabel *myLbl = [[UILabel alloc] init];
myLbl.frame = CGRectMake(0, 0, 300, 50);
[myLb1 setTag:1];
//myLbl.backgroundColor = [UIColor blueColor];
myLbl.text = #"Apple";
self.lbl_ = myLbl;
[self addSubview:myLbl];
}
return self;
}
in viewdidload
LabelView *lblView = [[LabelView alloc] init];
[lblView setFrame:CGRectMake(10, 100, 300, 50)];
self.cslbl_ = (UILabel*)[lblView viewWithTag:1];
[self.view addSubview:lblView];
Now you can set text to your self.cslbl_
I want to set the custom view's frame as my subview's frame, which is a UILabel. but
when I use self.frame the UILabel shows empty text. I need to hardcode the parameters in CGRectMake in order to see my UILabel. How do I set the UILabel's frame to be my custom view's frame?
- (id)initWithFrame:(CGRect)frame withText:(NSString *)text
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor grayColor];
self.layer.cornerRadius = 3;
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor blackColor].CGColor;
_text = text;
}
return self;
}
- (void)layoutSubviews
{
// This works
// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];
// This doesn't work
UILabel *label = [[UILabel alloc] initWithFrame:self.frame];
label.text = #"";
if (![self.text isEqualToString:#"?"]) {
label.text = self.text;
label.textAlignment = NSTextAlignmentCenter;
}
[self addSubview:label];
}
layoutSubviews will be invoked many times, so you should not addSubview like this. Better style is to set a property of UILabel, like your text property:
#property (nonatomic, strong) UILabel * label;
then change your layoutSubviews code:
- (void)layoutSubviews
{
if(!self.label){
self.label = [[UILabel alloc] initWithFrame:self.frame]; //Use ARC
self.label.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.label];
}
self.label.frame = self.bounds;
if (![self.text isEqualToString:#"?"]) {
self.label.text = self.text;
}
}
Try with following code.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 40)]; // set frame as you want
label.backgroundColor = [UIColor yellowColor]; // set color as you want
label.text = #"Hellow";
[yourParentView addSubview:label];
use this code
- (id)initWithFrame:(CGRect)frame withText:(NSString *)text
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor grayColor];
self.layer.cornerRadius = 3;
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor blackColor].CGColor;
_text = text;
[self layoutSubviews:frame];
}
return self;
}
- (void)layoutSubviews:(CGRect)myFrame
{
// This works
// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];
// This doesn't work
UILabel *label = [[UILabel alloc] initWithFrame:myFrame];
label.text = #"";
if (![self.text isEqualToString:#"?"]) {
label.text = self.text;
label.textAlignment = NSTextAlignmentCenter;
}
[self addSubview:label];
}
You do not want to create views inside the layoutSubviews method. That method can be called multiple times and you'll create each view multiple times (having them overlap each other).
Instead create the views you need in the initWithFrame method.
For the answer to your actual question, you probably need to set the frame of the new label to the bounds of the custom view. The bounds will have an origin of 0,0 and so it will work no mater where the custom view is placed in it's superview. If you use the frame, the label will be offset by however much the custom view is offset.
You can probably completely remove your layoutSubviews method as that's not the place to set the label text either. Then add this:
- (instancetype)initWithFrame:(CGRect)frame withText:(NSString *)text
{
self = [super initWithFrame:frame];
if (!self) return nil;
self.backgroundColor = [UIColor grayColor];
self.layer.cornerRadius = 3;
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor blackColor].CGColor;
_text = text;
UILabel *label = [[UILabel alloc] initWithFrame:self.bounds];
label.text = #"";
if (![self.text isEqualToString:#"?"]) {
label.text = self.text;
label.textAlignment = NSTextAlignmentCenter;
}
[self addSubview:label];
return self;
}
If you want the label to resize when the custom view resizes, you could do that inside the layoutSubviews by setting the label.frame = self.bounds;, but you'd be better off using autoresizing masks or auto layout.
I have a custom cell that I am controlling the color of when it is selected, ie the example code below:
UIView *selectedBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600, 600)];
[selectedBackground setBackgroundColor:[UIColor selectedCellColor]];
self.selectedBackgroundView = selectedBackground;
This works, however I would only like to have part of the cell change colors when selected. My custom cell is broken down into many different subviews, and I have it sectioned out where I would be able to define the specific view that I would like to change colors for.
How can I control the selectedBackgroundView, or use a different method, to have the background color change encompass a single subview in my cell?
Ya you are in the rite way,by subclassing the UITableView cell
hear is the sample code that you may find the answer for your question :)
//in subclassed cell class
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.frame = CGRectMake(0, 0, 334, 250);
UILabel *aLabel1= [[UILabel alloc]init];
UILabel *aLabel2 = [[UILabel alloc]init];
self.label1 = aLabel1;
self.label2 = aLabel2;
self.label1.text = #"Happy";
self.label2.text = #"coding";
UIImageView *bg1 = [[UIImageView alloc]init];
bg1.tag = 100;
UIImageView *bg2 = [[UIImageView alloc]init];
bg2.tag = 200;
[self addSubview:bg1]; // you must add your background views first
[self addSubview:bg2];
[self addSubview:label1];//then other views
[self addSubview:label2];
[aLabel1 release];
[aLabel2 release];
[bg1 release];
[bg2 release];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// Configure the view for the selected state
// hear only you can manage your background views, simply i am adding 2 imageviews by setting different colors
[super setSelected:selected animated:animated];
self.backgroundColor = [UIColor greenColor];
if(selected)
{
self.label1.backgroundColor = [UIColor redColor];
self.label2.backgroundColor = [UIColor brownColor];
UIImageView *bg1 = (UIImageView *)[self viewWithTag:100];
bg1.frame = CGRectMake(0, 0,334/2, 250);
bg1.backgroundColor = [UIColor yellowColor];
}
else
{
self.label1.backgroundColor = [UIColor brownColor];
self.label2.backgroundColor = [UIColor redColor];
UIImageView *bg2 =(UIImageView *) [self viewWithTag:200];
bg2.frame = CGRectMake(35, 0, 334/2, 250);
bg2.backgroundColor = [UIColor lightGrayColor];
}
}
-(void)layoutSubviews
{
//i am setting the frame for each views that i hav added
[super layoutSubviews];
self.label1.frame = CGRectMake(10, 10, 60, 35);
self.label2.frame = CGRectMake(65, 10, 60, 35);
}
hope helps u :)
note: i am using "without ARC"
I'm developing an iOS application with latest SDK.
I have this custom UIView:
#define ANNOTATION_WIDTH 250
#define ANNOTATION_HEIGHT 200
#implementation CustomView
- (id)initwithTitle:(NSString* )title subTitle:(NSString* )subTitle
{
CGRect annotationFrame =
CGRectMake(10, 10, ANNOTATION_WIDTH, ANNOTATION_HEIGHT);
if ([self initWithFrame:annotationFrame])
{
self.backgroundColor = [UIColor redColor];
self.opaque = YES;
/*
UILabel* nameLabel =
[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ANNOTATION_WIDTH, 20.0)];
nameLabel.backgroundColor = [UIColor whiteColor];
nameLabel.textAlignment = NSTextAlignmentCenter;
//nameLabel.text = coordinate.name;
nameLabel.text = title;
[nameLabel sizeToFit];
[nameLabel setFrame: CGRectMake(0,
0,
nameLabel.bounds.size.width + 8.0,
nameLabel.bounds.size.height + 8.0)];
[self addSubview:nameLabel];
UILabel* placeLabel =
[[UILabel alloc] initWithFrame:CGRectMake(25, 0, ANNOTATION_WIDTH, 20.0)];
placeLabel.backgroundColor = [UIColor whiteColor];
placeLabel.textAlignment = NSTextAlignmentCenter;
//placeLabel.text = coordinate.place;
placeLabel.text = subTitle;
[placeLabel sizeToFit];
[placeLabel setFrame:CGRectMake(25,
0,
placeLabel.bounds.size.width + 8.0,
placeLabel.bounds.size.height + 8.0)];
[self addSubview:placeLabel];
*/
}
return self;
}
I will show a title and a subTitle, but now that code is comment because I'm testing something.
To add this custom view to an UIViewController I do this:
#import "ViewController.h"
#import "CustomView.h"
#import <QuartzCore/QuartzCore.h>
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CustomView* cView = [[CustomView alloc] initwithTitle:#"Titulo" subTitle:#"Subtitulo"];
cView.layer.cornerRadius = 10;
cView.layer.masksToBounds = YES;
[self.view addSubview:cView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
If I don't show title and subTitle and I don't see my customView, but I show title and subTitle I only see rounded upper left corner.
I have tried this also, and it doesn't work:
CustomView* cView = [[CustomView alloc] initwithTitle:#"Titulo" subTitle:#"Subtitulo"];
cView.layer.cornerRadius = 10;
cView.clipsToBounds = YES;
Is there any way to show a UIView with rounded corners without adding any subview?
I don't know what it is happening and Id on't know why I get rounded upper left corner only.
Try this:
cView.layer.cornerRadius = 10.0f;
cView.layer.masksToBounds = YES;
cView.layer.opaque = NO;
[self.view addSubview:cView];
are you tried this
[self.layer setCornerRadius:10];
[self.layer setMasksToBounds:YES];
I am trying to add a custom view in the center of a navigation bar and I am using the following code to test it:
UIView * testView = [[UIView alloc] init];
[testView setBackgroundColor:[UIColor blackColor]];
testView.frame = CGRectMake(0, 0, 100, 35);
[self.navigationController.navigationItem.titleView addSubview:testView];
I am setting this up in the viewDidLoad method of my view controller but when i run my program
nothing seems to change in my navigation bar.
Could you help me with this?
This works. Give frame at the time of initialisation
UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setBackgroundColor:[UIColor whiteColor]];
self.navigationItem.titleView = iv;
If you want to just customize the title for one view controller you can use
UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = #"Diga-nos o motivo";
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor colorWithRed:77.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0];
lblTitle.shadowColor = [UIColor whiteColor];
lblTitle.shadowOffset = CGSizeMake(0, 1);
lblTitle.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18.0];
[lblTitle sizeToFit];
self.navigationItem.titleView = lblTitle;
or if you want to customize for all view controllers use
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Arial-Bold" size:10.0],
UITextAttributeFont,
nil]];
Replace
[self.navigationController.navigationItem.titleView addSubview:testView];
to
self.navigationItem.titleView = testView;
Edit:
Note: You cannot add subviews to titleView cause it's default value is nil, you need to set a new view as the titleView.
Swift 3/4
You may set i.e. UILabel as a titleView. Call it in viewDidLoad():
private func setNavigationTitle(_ title: String) {
navigationItem.title = nil // clear the default title
let titleLabel = UILabel() // you don't need to specify a frame, it will be centred in the navbar
titleLabel.font = ...
titleLabel.textColor = ...
titleLabel.text = title
titleLabel.backgroundColor = .clear
navigationItem.titleView = titleLabel
navigationTitleView = titleLabel // you may create a property if you want to manipulate the title view later
}
Note navigationItem.title = nil, otherwise title may override titleView.
CustomLabel *titleLabel = [CustomLabel initWithLabelFrame:labelFrame textFont:[UIFont fontWithName:#"Helvetica" size:[UIFont systemFontSize]] textColor:[UIColor blackColor] labelText:#"Add as" textAlignment:NSTextAlignmentCenter labelOnView:reference.view labelTag:62];
[self.navigationItem setTitleView:titleLabel]; // titleLabel set in navigationItem
#import <UIKit/UIKit.h>
#interface MasterViewController : UITableViewController
#end
#import "MasterViewController.h"
#interface MasterViewController ()
#end
#implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.titleView = [self titleView];
}
- (UIView *)titleView {
CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat width = 0.95 * self.view.frame.size.width;
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, navBarHeight)];
UIImage *logo = [UIImage imageNamed:#"logo.png"];
UIButton *logoButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGFloat logoY = floorf((navBarHeight - logo.size.height) / 2.0f);
[logoButton setFrame:CGRectMake(0, logoY, logo.size.width, logo.size.height)];
[logoButton setImage:logo forState:UIControlStateNormal];
UIImage *bubble = [UIImage imageNamed:#"notification-bubble-empty.png"];
UIImageView *bubbleView = [[UIImageView alloc] initWithImage:bubble];
const CGFloat Padding = 5.0f;
CGFloat bubbleX =
logoButton.frame.size.width +
logoButton.frame.origin.x +
Padding;
CGFloat bubbleY = floorf((navBarHeight - bubble.size.height) / 2.0f);
CGRect bubbleRect = bubbleView.frame;
bubbleRect.origin.x = bubbleX;
bubbleRect.origin.y = bubbleY;
bubbleView.frame = bubbleRect;
[containerView addSubview:logoButton];
[containerView addSubview:bubbleView];
return containerView;
}
#end
You'll want to use storyboard to do this to support iPhone 6 and newer (larger) devices.
Create a container view for your custom navigation item title/subtitle etc, and drag it into the visual editor (not into the view hierarchy).
Swift 3
let myImageView = UIImageView(image: <...set your image...>)
override fun viewDidLoad(){
super.viewDidLoad()
self.navigationItem.titleView = myImageView //
}
One more alternate solution is
override fun viewWillAppear(_ animated: Bool) {
super. viewWillAppear(animated)
self.navigationItem.titleView = myImageView
}
I recommend to use, viewDidLoad to setup your titleView