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;
Related
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
How to make UITextField on background of UITableView?
I want to set textField in the top and when i scroll tableView my textField don't move.
Something like this
- (void)viewDidLoad {
[super viewDidLoad];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 30, 100, 30)];
self.textField.backgroundColor = [UIColor redColor];
self.tableView = [[UITableView alloc] initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];
self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
[self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:NSStringFromClass([TableViewCell class])];
[self.view addSubview:self.textField];
}
You have another solution - besides one suggested in comments:
You can create UIViewController with tableView and text field. Assign delegate and dataSource to the view controller. Pin text field to the top and you have your desired behaviour. You will have to connect tableView with IBOutlets.
If you want to set textField in the top change textField frame
From
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 100, 30)];
To
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 30, 100, 30)];
self.textField.backgroundColor = [UIColor redColor];
[self.view addSubview:self.textField];
Also another solution is add textField to navigation item title.I tried this and it works fine.
textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 30, 100, 30)];
textField.backgroundColor = [UIColor redColor];
[self.view addSubview:textField];
self.navigationItem.titleView = self.textField;
So I have a tableview inside a UIPopOverController and I attached a UISearchBar to the header of the tableview and I have the UISearchDisplayController displaying the correct results. However, I can't get the results to display within the UIPopoverController window. It's displaying behind the popover view into the DetailView covering half the screen (SplitViewController for iPad). How do I bring the results/tableview into the popover view? I tried adding the searchTableView to the popover view, but it just covers up the original tableview.
Here is the code of setting this up:
//Initialize Popover Controller
UIView *ccPopoverView = [[UIView alloc] init];
UIViewController* popoverTableContent = [[UIViewController alloc] init];
self.ccTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 320, 400) style:UITableViewStylePlain];
self.ccTableView.dataSource = self;
self.ccTableView.delegate = self;
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 44.0)];
self.searchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.delegate = self;
searchBar.frame = CGRectMake(0, 0, 0, 38);
self.ccTableView.tableHeaderView = searchBar;
ccPopoverView.backgroundColor = [UIColor blackColor];
//Here is where I tried adding the searchResultsTableView as a subview.
//[ccPopoverView addSubview:self.searchDisplayController.searchResultsTableView];
[ccPopoverView addSubview:self.ccTableView];
popoverTableContent.view = ccPopoverView;
self.popoverTableController.delegate = self;
self.popoverTableController = [[UIPopoverController alloc] initWithContentViewController:popoverTableContent];
[self.popoverTableController setPopoverContentSize:CGSizeMake(320, 440) animated:NO];
Thanks!
I fixed the issue instead of adding self when I initialized the search bar, I needed to put in the popovercontroller as the initialization.
I have two views in my program. One is statusView, it's alpha is set to 0.8f. And below it is tableView. How do I disable statusView such way, that clicking on it would not click tableView. Settings userInteractionEnabled = false; didn't help.
Here is how i add views.
[self.view addSubview:_builded.view]; // obj with my tableView
info = [[Info alloc] init];
[self.view addSubview:info.view]; // statusView
If you add a subview like the following, the tableView underneath would not catch the user interaction.
UITableView *testTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
testTableView.delegate = self;
testTableView.dataSource = self;
[self.view addSubview:testTableView];
UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
statusView.backgroundColor = [UIColor redColor];
statusView.alpha = 0.8;
[self.view addSubview:statusView];
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.