Placing a UITextField in the center of the view - iphone

I have a UITextField and I want to center this field on the center of the view. Any ideas?
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, 200, 28)];
Instead of CGRectMake(some numbers) - I want to place it in the center of the view.

You can directly assign it the center of the desired view:
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, 200, 28)];
usernameField.center = myView.center;

userNameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 28)];
userNameField.center = CGPointMake(CGRectGetWidth(someView.bounds)/2.0f, CGRectGetHeight(someView.bounds)/2.0f);
Where someView is the superview of userNameField.

Why not use the view's center property?
userNameField.center = parentViewField.center;

Related

How to remove text after drawInRect?

I have a UIView subclass called BackgroundText to draw some text.
-(void) drawRect:(CGRect)rect
{
[#"synchronized." drawInRect:CGRectMake(0, 29, 320, 60) withFont:bigFont lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentRight];
}
--
backgroundText = [[MMRoomBackgroundText alloc] initWithFrame:CGRectMake (0, 142 + 44, 320, 80)];
[self.view addSubview:backgroundText];
I expect [backgroundText removeFromSuperview]; can remove these text from screen,but it doesn't work.
Thanks.
You need to call setNeedsDisplay on the view and check.
I hope this will help you.
Here LoadingBG is My View And LoadingText is My Label And I am Putting Text in Label and Add Label into View and After that I remove View From SuperView When I dont Need Text.
For Adding Text:
UIView * LoadingBG = [UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460);
UILabel *LoadingText = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)] autorelease];
LoadingText.text = #"Loading...";
LoadingText.backgroundColor = [UIColor clearColor];
[LoadingBG addSubview:LoadingText];
For Remove Text:
[LoadingBG removeFromSuperview];
And Here I had Use Fixed Size For View And Label. You can Use What You want as a frame.

Programmatically add UILabel to toolbar

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!

UIView subviews are not visible

I create an UIView in applicationDidFinishLaunchingWithOptions method:
CGRect inputTextViewRect = CGRectMake(0, 420, 320, 80);
UIView *inputTextView = [[UIView alloc] initWithFrame:inputTextViewRect];
inputTextView.backgroundColor = [UIColor grayColor];
Then I create two subviews - one UIButton and one UITextView:
UITextView *inputTextField = [[UITextView alloc] initWithFrame:CGRectMake(10, 450, 230, 20)];
UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect buttonFrame = CGRectMake(250, 450, 50, 20);
My problem is if I add my inputTextField and sendButton as subviews to main window - they will be visible and working. If I add them to my UIView:
[inputTextView addSubview:inputTextField];
[inputTextView addSubview:sendButton];
And then add this view to main window:
[self.window addSubview:inputTextView];
In this case i see only gray background of inputTextView and no subviews that were added before.
Could you please help me figure out whats wrong with it?
The problem is with your CGRectMake call:
CGRectMake(x, y, width, height);
x and y and are coordinates where you want to place the element over the super view, so you have the following, which is incorrect:
CGRect inputTextViewRect = CGRectMake(0, 420, 320, 80);
UITextView *inputTextField = [[UITextView alloc] initWithFrame:CGRectMake(10, 450, 230, 20)];
CGRect buttonFrame = CGRectMake(250, 450, 50, 20);
Try changing those to
CGRect inputTextViewRect = CGRectMake(0, 0, 320, 80);
UITextView *inputTextField = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 230, 20)];
CGRect buttonFrame = CGRectMake(0, 0, 50, 20);
After changing this you will be able to see UITextView and UIButton, then you make the x and y position changes according to your need.
Actually when you add inputTextField subview of inputTextView, it will be placed at y position of 450 inside inputTextView. But that itself is of height 80 only.
So when you add that into inputTextView, make the origin y position to 30.
So, if you add to main view, it will be like
UITextView *inputTextField = [[UITextView alloc] initWithFrame:CGRectMake(10, 450, 230, 20)];
Else if you add it to inputTextView, then
UITextView *inputTextField = [[UITextView alloc] initWithFrame:CGRectMake(10, 30, 230, 20)];

Change the Width of UISearchBars on a TableView

I am required to create two UISearchBars in my tableView. I want both of them of equal width on top of the table (side-by-side).
I have created two outlets of UISearchBar, and property and de alloc for them. I am finding it hard to place (I mean fit) both of them in the view. I get only one search bar expanding to the FULL width of the screen. How do I fit two ?
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 4, 45)];
zipSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(4, 0, 4, 45)];
searchBar.placeholder = #"School Name";
zipSearchBar.placeholder=#"Zip";
searchBar.delegate = self;
zipSearchBar.delegate = self;
self.tableView.tableHeaderView= zipSearchBar;
self.tableView.tableHeaderView= searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
You are simply reassigning it when you do self.tableView.tableHeaderView= searchBar;. You will have to build a view that contains both the search bars and then set it to the table's header view. Something like this,
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
zipSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(200, 0, 120, 45)];
searchBar.placeholder = #"School Name";
zipSearchBar.placeholder=#"Zip";
searchBar.delegate = self;
zipSearchBar.delegate = self;
UIView * comboView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
[comboView addSubview:searchBar];
[comboView addSubview:zipSearchBar];
self.tableView.tableHeaderView= comboView;
[comboView release];

UITextField Black Pixels at Edges

I've created a UITextField programmatically with the following code:
self._maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, labelWidth, labelHeight)];
self._maxPriceField.borderStyle = UITextBorderStyleRoundedRect;
self._maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
self._maxPriceField.font = fieldFont;
self._maxPriceField.delegate = self;
The problem I'm having is that my UITextField ends up having these strange black pixels on the edges. This happens both on the device and in the simulator. You can see in the screenshot below:
When I create the same UITextField using IB, with the same specs and background, I have no problem. Unfortunately I need to create this UITextField programmatically.
Has anybody seen this before? What to do?
It seems this is the way textfields are drawn on the screen. I played around with your code a little bit, and if I set the text field height lower than about 20, you start to see a noticeable "shadow" that is clearly not drawn correctly.
My suggestion is to either use a height of 20 or higher for the text field, or to use a different style, such as Bezel or Line and set the background to white.
Here is a screenshot to demonstrate:
And here is the code I used to draw these:
int labelWidth = 100;
int labelHeight = 10;
_maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, labelWidth, labelHeight)];
_maxPriceField.borderStyle = UITextBorderStyleRoundedRect;
_maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
//_maxPriceField.font = fieldFont;
//_maxPriceField.delegate = self;
[self.view addSubview:_maxPriceField];
UITextField *secondField = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, labelWidth, labelHeight + 10)];
secondField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:secondField];
[secondField release];
UITextField *thirdField = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, labelWidth, labelHeight + 20)];
thirdField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:thirdField];
[thirdField release];
UITextField *fourthField = [[UITextField alloc] initWithFrame:CGRectMake(10, 110, labelWidth, labelHeight + 30)];
fourthField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:fourthField];
[fourthField release];
UITextField *noRoundFirst = [[UITextField alloc] initWithFrame:CGRectMake(10, 160, labelWidth, labelHeight)];
noRoundFirst.borderStyle = UITextBorderStyleBezel;
noRoundFirst.backgroundColor = [UIColor whiteColor];
[self.view addSubview:noRoundFirst];
[noRoundFirst release];
Hope this helps.
Mk