How to make UITextField appear with Swift Code - ios

I want to make the following UITextField appear when I compile and run the application. I have the UITextField declared and initialised as:
var myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200.00, height: 40.00));
However when I run the app, this text field does not appear. What Swift code would make the textfield appear?

You have to actually add the text field to the view hierarchy, which is done slightly differently depending on the class that you're in. If for example, you're in a UIViewController subclass, you add it as a subview of the UIView attached to the view controller's view property.
var myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200.00, height: 40.00));
self.view.addSubview(myTextField)
Or, if you're working with a subclass of UIView, you could use:
self.addSubview(myTextField)
After you've added your text field to the view hierarchy, it's still possible that you won't be able to see it. By default, text field will have a white background color, no text, and no border around it. This can all be changed through use of the following properties.
myTextField.backgroundColor = UIColor.redColor()
myTextField.text = "some string"
myTextField.borderStyle = UITextBorderStyle.Line

You need to add this UITextField as a subview of another UIView.
Assuming you are inside a UIViewController, you could call self.view.addSubview(myTextField).

Related

UIView added to SKScene have strange border

I need to add UIView with white background (have HUD class that is used through out the app) to the SKScene with white background. The code is something like this:
class CategoriesGameScene: SKScene {
override func didMove(to view: SKView) {
let view = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 50))
view.backroundColor = UIColor.white
}
}
Everything works, however there is a problem - on the border of the UIView there is small black/greyish border (so two views are not "blending") with each other. The funny thing is when I am trying to make a screenshot (so I can post an example here) - there is no border on the screenshot and everything looks as it should and also if i am tapping homebutton to multitasking view on the iphone line is also disappearing. Is there a proper way to add UIView to SKScene or can I try something different to get rid of that?
You can resolve this issue by checking "Allow Transparency" option of SKView properties in Storyboard.

UITextField as title view in UITableViewControllers embedded in a UIPageViewController

I have a UIPageViewController whose pages are a bunch of UITableViewController. I want to use a UITextField as the title field for each of my table view.
In my UIPageViewController's viewDidLoad, I have the following:
let titleField = UITextField(frame: CGRect(x: 0, y: 0, width: 150, height: 21))
titleField.delegate = self
titleField.placeholder = "(table name)"
navigationItem.titleView = titleField
This works and the text field shows up, but I'd like each of the page (i.e UITableViewController) to handle its own title view. So I moved the above code into my UITableViewController's viewDidLoad, but now nothing shows up.. Why?
Any input would be great. I guess I could keep track of which page I'm currently in in my UIPageViewController but I'm trying to avoid that. It's a lot of bookkeeping that I don't need.

Change height of navigation bar ios

I am trying to change the height of the navigation bar with this code provided in few links over stackoverflow, but it is not working as required.
self.navigationController?.navigationBar.frame = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.size.width, height: 200)
self.navigationController?.navigationBar.layoutIfNeeded()
Is there anything additional that I need to add to make it work ?
I did checked the reference link too but it is not working, also it is the same as I written with added fix.
The hierarchy of UINavigationBar view is private so we can't manipulate it.We create our custom view and add into a Viewcontroller.

Draw custom view within UITextField

I want to extend UITextField to draw inputed text in a custom UILabel like class that draws certain symbols in a special way. Would I have to modify the drawing code directly or is there a way to add this view "on top" of the UITextField while still being able to input text into the UITextField?
This should add a view to a text field. Now just change thisView to any custom view you want with any custom frame you want and pow! You got what you want ;)
let frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let thisView = UIView(frame: frame)
thisView.backgroundColor = UIColor.blue
textField.addSubview(thisView)

Swift - UISearchBar in UIToolbar

How can I create A UISearchBar inside of a Toolbar in Swift? If I use the Interface Builder I get the following error:
error: Illegal Configuration: UISearchBar embedded in UIBarButtonItems (Only available in iPad documents)
Is there a Swift solution for this problem? The Toolbar will only be available on iPad.
UIToolBar is just a custom UIView, so you have three options (that I can see) for your implementation:
First, if you want to stick the search bar in the toolbar here's the code:
var searchBar = UISearchBar(frame: CGRectMake(0, 0, 100, 50))
var myView = UIView(frame: searchBar.frame)
myView.addSubview(searchBar)
var barButtonItem = [UIBarButtonItem(customView: myView)]
toolBar.setItems(barButtonItem, animated: false)
Second, if you want the search bar in the nav bar on your screen you can do the following:
let searchBar = UISearchBar(frame: CGRectMake(0, 0, 300, 50))
navigationController?.navigationBar.addSubview(searchBar)
Third, if you need the toolbar to be elsewhere, you can create your own custom UIView that looks like a toolbar (spans the width of the screen) and add the search bar to that. You can even make this new toolbar of yours a custom class so it's reusable throughout your program.
Cheers!

Resources