I created a UILabel programatically and triggering a button I would like to hide the same label. This is my code:
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
nameLabel.text = #"TEXT";
nameLabel.backgroundColor = [UIColor greenColor];
nameLabel.numberOfLines = 5;
nameLabel.font = [UIFont boldSystemFontOfSize:12];
[self.view addSubview:nameLabel];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Hide" style:UIBarButtonItemStyleBordered target:self action:#selector(back)];
- (IBAction)back{
self.navigationItem.rightBarButtonItem=nil;
[nameLabel setHidden: YES]; not working
nameLabel.hidden = YES; not working
}
Am I missing something?
This is also another way to do same
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
nameLabel.text = #"TEXT";
nameLabel.tag = 1001;
nameLabel.backgroundColor = [UIColor greenColor];
nameLabel.numberOfLines = 5;
nameLabel.font = [UIFont boldSystemFontOfSize:12];
[self.view addSubview:nameLabel];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Hide" style:UIBarButtonItemStyleBordered target:self action:#selector(back)];
- (IBAction)back{
self.navigationItem.rightBarButtonItem=nil;
UILabel *tempLabel = (UILabel *)[self.view viewWithTag:1001];
[tempLabel setHidden: YES];
tempLabel.hidden = YES;
}
In order for the button to be accessible from other methods, you need to assign it to an instance variable (whether directly or via a property) rather than assigning it to a local variable. The proper way to declare the property is
#property(nonatomic, strong) UILabel *nameLabel;
which you can then assign to using
self.nameLabel = [[UILabel alloc] init...];
Later on, you can say
self.nameLabel.hidden = YES;
and it should work.
It's hard to know how that would even compile since the code you show to create nameLabel makes it local to whatever method that's in. Try making nameLabel a property and using self.nameLabel whenever you reference it, either creating it or touching its properties.
Related
myview = [[AVPlayerViewController alloc]init];
[window.rootViewController.view addSubview:controller.view];
UILabel *label = [[UILabel alloc] init];
label.text = #"TEST";
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Menlo" size:(72.0)];
[myview.contentOverlayView addSubview:label];
myview.view.frame = window.rootViewController.view.frame;
// Disable playback controls
myview.showsPlaybackControls=false;
// show the view controller
[window.rootViewController addChildViewController:myview];
myview is playing video, but the "TEST" label isn't visible. I'm not sure what I'm doing wrong.
hi i'm begginer in objective-c and i want to learn how i can make UILabel in center of screen
this is my code:
XXRootViewController.h
#interface XXRootViewController : UIViewController{
UILabel *title; } #end
XXRootViewController.m
#import "XXRootViewController.h"
#implementation XXRootViewController {
NSMutableArray *_objects;
}
- (void)loadView {
[super loadView];
self.view = [[[UIView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]]
autorelease];
self.view.backgroundColor = [UIColor whiteColor];
title = [[UILabel alloc] initWithFrame:CGRectMake(300,200,400,200)];
title.text = #"This is Sasuke's first app :)";
[self.view addSubview:title];
}
#end
Try this code i make some changes in you code
self.view = [[UIView alloc]
initWithFrame:[UIScreen mainScreen].bounds];
self.view.backgroundColor = [UIColor whiteColor];
title = [[UILabel alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width - 50,200)];
title.text = #"This is Sasuke's first app :)";
title.textAlignment = NSTextAlignmentCenter;
title.center = self.view.center;
[self.view addSubview:title];
You just need to set frame in center of the screen ...
change your one line code with it -
title = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 200,self.view.frame.size.height/2 - 100,400,200)];
now your label is shown in the center of screen .. This will help you change the frame according to your use...
Has Hardik Thakkar said, the best way would be to use the center method. That way, even if your label would grow, it will always be centred.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(300,200,400,200)];
label.text = #"something";
label.center = self.view.center; (this should always be you superview)
Now, even if your label is bigger than the size you were expecting, it will always be centred
hi Im programmatically creating an UILabel like this
`
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[[UILabel alloc] init] initWithFrame:CGRectMake(0.0, _photoView.frame.size.height, _photoView.frame.size.width, 100.0)];
// _titleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
_titleLabel.backgroundColor =[UIColor clearColor];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
_titleLabel.textAlignment = NSTextAlignmentRight;
[self addSubview:_titleLabel];
}
return _titleLabel;
}`
this _photoView is an UIImageView I have created already. I want to change the UILabel View Y value. But the problem is when I change this second parameter label y position is not changing. Any one can tell e the reason for this.
And this is how I created the ImageView
`
- (UIImageView *)photoView {
if (!_photoView) {
_photoView = [[UIImageView alloc] init];
_photoView.contentMode = UIViewContentModeScaleAspectFill;
_photoView.clipsToBounds = YES;
_photoView.layer.cornerRadius = 5;
_photoView.clipsToBounds = YES;
[self addSubview:_photoView];
}
return _photoView;
}`
Thanks
In this line , you have used two types init
[[[UILabel alloc] init] initWithFrame:CGRectMake(0.0, _photoView.frame.size.height, _photoView.frame.size.width, 100.0)]
change this line to
[[UILabel alloc] initWithFrame:CGRectMake(0.0, _photoView.frame.size.height, _photoView.frame.size.width, 100.0)]
I want to set a custom view to the inputView property of a UITextField. (Basically because I want to add a UILabel to the selectionIndicator of a UIPickerView and I couldn't find an easier way...) If I set a UIPickerView as the inputView, everything works perfectly normal. But if I set a UIView as the inputView, it just doesn't show up when the UITextField becomes the firstResponder. Has anybody got an explanation for me here?
This works:
UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];
// configuration of field
UIPickerView* pickerView = [[UIPickerView alloc] initWithFrame:field.inputView.frame];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
field.inputView = pickerView;
This doesn't:
UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];
// configuration of field
UIView* view = [[UIView alloc] initWithFrame:field.inputView.frame];
view.backgroundColor = [UIColor redColor];
field.inputView = view;
I am not sure why the following code is not working:
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:#"New" style:UIBarButtonItemStyleBordered target:self action:#selector(newClicked:)];
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(share:)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:newButton, shareButton, nil];
It only display the "newButton" UIBarButtonItem on the navigation bar and not the "shareButton" button.
I found a different issue involving this behavior.
If you've set a custom titleView (navigationItem.titleView = ...) then the navigation item can sometimes hide your buttons except the first one if it doesn't have room.
So make sure you limit the width of your custom titleView.
rightBarButtonItems is in ios 5.
I think the issue is with the objet name new, please change this to any other name like newButton or something like that.
Because new is a keyword used for memory allocation in C++
Looks like the problem had to do with code following the questioned code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"Marker Felt" size:26.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;
self.navigationItem.titleView = label;
[label release];
Posting this now in case someone else encounters this problem!