I'm trying to add a textfield into a navigation bar, but it doesn't show up in the simulator. I do it as follows inside viewdidload:
UITextView *textField = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 170, 44)];
self.navigationItem.titleView = textField;
Any ideas?
Thanks!
i have done it in this way. i know that you have got to the answer uptil now
but still this will helpful to someone else so
this can be done as follows
-(void)viewWillAppear:(BOOL)animated
{
imgVw =[[UIImageView alloc]initWithFrame:CGRectMake(68, 7, 180, 28)];
[imgVw setImage:[UIImage imageNamed:#"textfieldBackground.png"]];
[self.navigationController.navigationBar addSubview:imgVw];
textfieldTxt = [[UITextField alloc]initWithFrame:CGRectMake(78, 11, 170, 25)];
textfieldTxt.backgroundColor = [UIColor clearColor];
[textfieldTxt setAutocorrectionType:UITextAutocorrectionTypeNo];
textfieldTxt.delegate = self;
[self.navigationController.navigationBar addSubview:textfieldTxt];
}
please don't forgot to remove from superview, when viewdidunload
- (void)viewDidUnload
{
[super viewDidUnload];
[textfieldTxt removeFromSuperview];
[imgVw removeFromSuperview];
}
thanks
Related
How to add a textfields dynamically on clicking the button?
when clicking on the button textfield should appear with scrollview.
can anyone explain me how to do?
what have you tried??
May be this is what you are asking for.
-(IBAction)buttonClicked:(id)sender
{
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(20, 20, 40, 21)];
[scrollView addSubview:textField];
[self.view addSubview:scrollView];
}
EDIT
IF not dynamically,
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(20, 20, 40, 21)];
[scrollView addSubview:textField];
[self.view addSubview:scrollView];
[scrollView setHidden:YES];
}
-(IBAction)buttonClicked:(id)sender
{
[scrollView setHidden:NO];
}
You can use below code to add Text Field programmatically..
UITextField * aTextfield = [[UITextField alloc] initWithFrame:frame]; // give your frame
aTextfield.borderStyle = UITextBorderStyleRoundedRect;
aTextfield.textColor = [UIColor blackColor];
aTextfield.font = [UIFont systemFontOfSize:17.0];
aTextfield.placeholder = #"Suchen";
aTextfield.backgroundColor = [UIColor clearColor];
aTextfield.autocorrectionType = UITextAutocorrectionTypeNo;
aTextfield.keyboardType = UIKeyboardTypeDefault;
aTextfield.returnKeyType = UIReturnKeySearch;
aTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
aTextfield.delegate = self;
[scrollview addSubview:aTextfield];
In Button action, you can create the dynamic textfields and them to your scrollview.....
Take a look at my code.
http://rajneesh071.blogspot.in/2012/09/make-textfield-and-label-dynamically-in.html
According to this you can create number of textfield and label in scroll view, so you can change it according to your requirement.
I am trying to add label (headerLabel) to another subview (introPadTutorial) here and it is not showing up on the screen. Can some please help me where am I making mistake?
//Header file
#property (nonatomic, retain) UIView *introPadTutorial;
//Implementation file
#synthesize introPadTutorial;
UIView *v = [_appDelegate getCurrentView];
CGRect tutorialFrame = [self getTableViewFrame];
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width, 80, -tutorialFrame.size.width, v.frame.size.height)];
[introPadTutorial setBackgroundColor:[UIColor uberLightGray]];
[introPadTutorial setUserInteractionEnabled:YES];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 180)];
[headerLabel setText:#"test test test"];
[headerLabel setBackgroundColor:[UIColor greenColor]];
[headerLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
[headerLabel setTextColor:[UIColor redColor]];
[headerLabel setShadowColor:[UIColor whiteColor]];
[headerLabel setShadowOffset:CGSizeMake(0, 1)];
[self.introPadTutorial addSubview:headerLabel];
[headerLabel release];
[v addSubview:introPadTutorial];
[introPadTutorial release];
Don't use a negative width to change the POSITION, use the origin to change the position.
Use this:
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width - tutorialFrame.size.width, 80, tutorialFrame.size.width, v.frame.size.height)];
I think that should help make the subview actually show up.
May be your...
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width, 80, -tutorialFrame.size.width, v.frame.size.height)];
should actually be...
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(0, 80, tutorialFrame.size.width, v.frame.size.height)];
Is your introPadTutorial view showing up? You have a negative width in it's initWithFrame call.
I am trying to add a UILabel programmatically into my UIToolBar but it dose not seem to be appearing. This is what I am doing with my code.
- (void) viewWillAppear:(BOOL)animated
{
// Create custom toolbar at top of screen under navigation controller
[matchingSeriesInfoToolBar setFrame:CGRectMake(0, 60, 320, 30)];
matchingSeriesInfoToolBar = [UIToolbar new];
[matchingSeriesInfoToolBar sizeToFit];
CGFloat toolbarHeight = 30;
CGRect mainViewBounds = [[UIScreen mainScreen] applicationFrame];
[matchingSeriesInfoToolBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds), 0, CGRectGetWidth(mainViewBounds), toolbarHeight)];
matchingSeriesInfoToolBar.tintColor = [UIColor darkGrayColor];
[self.view addSubview:matchingSeriesInfoToolBar];
// Create size of uitableview (to fit toolbar.
[matchingSeriesTableView setFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height - 30)];
[self.view addSubview:matchingSeriesTableView];
// Ad UILabel to the toolbar
UIBarButtonItem *textFieldItem = [[UIBarButtonItem alloc] initWithCustomView:manufSelectionLabel];
matchingSeriesInfoToolBar.items = [NSArray arrayWithObject:textFieldItem];
manufSelectionLabel.text = #"Hello World!";
[super viewWillAppear:animated];
}
So pretty much I have created a custom toolbar which I have changed the usual location from the bottom of the screen, to appear under the UINavigationController, this is also added to the view like this so it animated properly in the view transitions..
After which I create the size of the tableview so that it appears after the custom toolbar..
then from there I am trying to add a UILabel to the toolbar.. but for some reason its not working out.
Any help would be greatly appreciated.
You must actually create the label somewhere. This code works just fine.
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:toolbar];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
label.backgroundColor = [UIColor clearColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:label];
toolbar.items = [NSArray arrayWithObject:item];
label.text = #"Hello World";
In the code you posted you don't ever make a UILabel. Your comment says Ad UILabel to the toolbar but you then proceed to make a UIBarButtonItem with a custom view manufSectionLabel.
Where is the code which creates manufSectionLabel?
PS This line does nothing :
[matchingSeriesInfoToolBar setFrame:CGRectMake(0, 60, 320, 30)];
because at that point matchingSeriesInfoToolbar is nil - you haven't made it yet!
I'm trying to add a button to a UIview, to cover the whole area of the view.. like this:
-(MBNHomeScreenButton*) initWithImage:(UIImage*)image
andHighlightedImage:(UIImage*)highlightedImage
andTitle:(NSString*)aTitle
withSelector:(SEL)actionButton
forDelegate:(UIViewController*)viewController{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, 100, 120);
self.imageView = [[UIImageView alloc] initWithImage:image highlightedImage:highlightedImage];
self.imageView.center = self.center;
self.imageView.frame = CGRectMake(10, 0, HS_BUTTON_IMAGE_WIDTH, HS_BUTTON_IMAGE_HEIGHT);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.highlighted = NO;
[self addSubview:self.imageView];
self.title = [[UILabel alloc] initWithFrame:CGRectMake(0, HS_BUTTON_IMAGE_HEIGHT, HS_BUTTON_IMAGE_WIDTH + 20, 30)];
self.title.text = aTitle;
self.title.textAlignment = UITextAlignmentCenter;
self.title.textColor = [UIColor whiteColor];
self.title.backgroundColor = [UIColor clearColor];
[self addSubview:self.title];
// This creates a transparent button over the view
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//button.backgroundColor = [UIColor redColor];
[button setFrame:self.frame];
[button addTarget:viewController action:actionButton forControlEvents:UIControlEventTouchUpInside];
[self addSubview: button];
}
return self;}
In the ViewController I create the View like this:
m_newsHSButton = [[MBNHomeScreenButton alloc] initWithImage:[UIImage imageNamed:NEWS_SHADED_IMAGE]
andHighlightedImage:[UIImage imageNamed:NEWS_HIGHLIGHTED_IMAGE]
andTitle:#"News"
withSelector:#selector(newsButtonPressed)
forDelegate:self];
m_newsHSButton.center = CGPointMake(m_backgroundView.frame.size.width / 4, m_backgroundView.frame.size.height / 4);
[backgroundImgView addSubview:m_newsHSButton];
Still... the newsButtonPressed doesn't get called.
Can you help?
Thanks!
====================
[edit]
Ok... figured it out.. Thanks guys.
It was the fact that I was trying to add the view as a subview to a UIImageView with the last line:
[backgroundImgView addSubview:m_newsHSButton];
Apparently, it won't get touches anymore.
Thanks for the effort guys!
Try making the button a #property or instance variable in your custom view. Then call [m_newsHSButton.button addTarget:self action:actionButton forControlEvents:UIControlEventTouchUpInside]; from the viewController.
I am using the code posted below to add Search bar to Navigation bar.
I am getting everything to show up correctly but there is a background (mostly of the UIBarButtonItem that I am not able to get rid of). - Please check the screenshot for iPad.
Is there a way to get rid of blue backgorund showing behind the search bar?
Thanks
Dev.
- (void) viewDidLoad {
[super viewDidLoad];
UIView *hackView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 250, 30)];
hackView.backgroundColor = [UIColor clearColor];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 250, 30)];
//[searchBar sizeToFit];
[searchBar setBackgroundColor:[UIColor clearColor]];
[hackView addSubview:searchBar];
[searchBar release];
UIBarButtonItem *hackItem = [[UIBarButtonItem alloc] initWithCustomView:hackView];
[hackItem setWidth:250];
self.navigationItem.rightBarButtonItem = hackItem;
[hackView release];
[hackItem release];
}
I believe the problem is that you are adding that extra UIView. Even though it has clearColor background, it is capturing that CGRect created for the UIView *hackView. Get rid of it and simply add the UISearchBar to the UIBarButtonItem and it should solve the display issue.
Also, don't forget to implement the UISearchBarDelegate for proper functionality.
I have solved this problem using following code.
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
UIBarButtonItem *navRight = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
[[self navigationItem] setRightBarButtonItem:navRight];
[searchBar release];
[navRight release];