iOS - Location of a TextField created programmatically - ios

I have a doubt: it is wrong to create a TextField from the code and not from interface builder?
For example, I am using 'AwsomeTextField' taken by CocoaPods.
In the pages of explanation says:
let field = AwsomeTextField(frame: CGRect(x: 60, y: 200, width: 320, height: 44))
field.placeholder = "test"
view.addSubview(field)
But if I assign an absolute position with CGRect (x: 60, y: 200, width: 320, height: 44) then if I run the app on an iPad or an iPhone IF of course the graphics becomes atrocious.
I can use the modules of cocoa pod from interface builder to add the right constraints, or can I work on autolayout also by code? I'm confused!

There is nothing to stop you adding constraints to a view created in code:
let field = AwsomeTextField(frame: .zero) // Create the view with a zero frame
field.placeholder = "test"
field.translatesAutoresizingMaskIntoConstraints = false // You should always do this to views you create in code that you want to apply layout constraints to
view.addSubview(field)
// No that you have added field as a subview, you can add constraints to it here.
If you want to use Interface Builder, drag a plain UIView from the picker onto your view controller and add any constraints. Then, set the class of the view from UIView to AwesomeTextField

'AwsomeTextField' Pod is an extension of a UITextField.
Drag a UITextField object in interface builder, make an outlet and switch the outlet type to AwesomeTextField like that:
#IBOutlet weak var awesomeTextField: AwsomeTextField!
also include AwsomeTextField class in Custom Class section of the Identity Inspector in xCode.
This way you don't need to create it from code all the time ;]

Related

How to define a class and module for a view created programmatically in swift

Let's say I create a view like this:
let myView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.view.addSubview(myView)
How can I give that view a custom class and module like I would in IB?
I am either searching for the wrong thing, or no one has asked this question. I also haven't tried anything because I can't figure out where to even start.
Update:
This is what I mean by adding a class and module like you would in IB:
Simply instantiate it instead of UIView.
Assuming this is your custom view:
class MyCustomView: UIView {
//...
}
Here is how to instantiate it:
let myView = MyCustomView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.view.addSubview(myView)
The module is the module your source file of class MyCustomView: UIView... is member of. When developing an iOS application (not a framework or other target types) this is your app. You can choose the "Target Membership" in Xcodes Inspector when selecting a source file:

Swift - SWRevealViewController menu placed after the status bar

I'm using SWRevealViewController menu and I have a header view for the table. You can see the table controller view structure and constrains:
And this is how my menu looks like
but I need it to be like
I tried :
self.profileHeader.frame = CGRect(x: 0, y: -20, width: profileHeader.frame.width, height: profileHeader.frame.height + 20)
But nothing changed.
Try using a normal UIViewController, and adjust constraints for the tableView manually check top constraint linked to view and not to top layout guide.
I hope this helps you, let me know about

How do I create a new View (or subView) with the tap of a button in swift?

I am currently making a flashcard app, and I am trying to make a button that creates an entirely new View (or subView) for the user to edit. Should I use Container Views? Collection Views? I also want to have these set to only one View Controller, so I can save the "cards" in one "set". Please Help!!
Edit: How can I save these views in a "folder" so the user can look at them later. Is there an efficient way to do this so the app doesn't slow or stop.
Edit #2: Ok, so I'm kind of getting it... collection views. But how would I implement this into my because I am using tvOS. Any thoughts?
If you want to create a new UIView programmatically, it's actually quite simple. You could create a method like this:
func addNewView(to container: UIView) {
let newView = UIView()
container.addSubview(newView)
newView.backgroundColor = UIColor.blue
newView.frame = CGRect(x: 10, y: 50, width: 200, height: 250)
}
That would create a new view inside whichever container view you passed in with a blue background, 10pts from the left (x-axis), 50pts from the top (y-axis, think a normal cartesian coordinate system with the y-axis inverted), width of 200 and height of 250.
You could then call this method on the push of a button by handling a button tap with it's own method like this:
func buttonTapped(_ sender: UIButton) {
addNewView(to: self.view)
}
Obviously all the values for the frame I gave you were just for an example so you could visualize it in your head, you can edit those however you want, or make calculations based on the size of your device's screen. You can get the device's screen size by saying self.view.bounds

Issue with section header and table view cell in UITableViewController

I've done an experiment of creating a screen with the use of UITableviewController instead of using scroll view reason is the fields in screen may get dramatically change that's why I do that
Screen :
All of those cells are static cells.
section header code
let backgroundImage = UIImage(named: "main_bg")
let imageView = UIImageView(image: backgroundImage)
self.tableView.backgroundView = imageView
header.backgroundColor = UIColor.clear
let lblheader : appLabel = appLabel(frame: CGRect(x: 0, y: 20, width: UIScreen.main.bounds.width, height: header.frame.height - 20))
lblheader.text = "SIGN UP"
lblheader.textFontsize = 14
lblheader.textFontType = 2
lblheader.textFontColor = 1
lblheader.textAlignment = .center
header.addSubview(lblheader)
let btnback : appButton = appButton(frame: CGRect(x: 0, y: lblheader.frame.origin.y, width: 50, height: lblheader.frame.height))
btnback.backgroundColor = UIColor.clear
btnback.setImage(UIImage(named: "back_arrow"), for: .normal)
btnback.addTarget(self, action: #selector(self.btnBackTapped), for: .touchUpInside)
header.addSubview(btnback)
Now when I scroll the table It looks as below:
I want to scroll static cell of the table under header section.
I know I can also use combination of UIViewController which contains Container and represents the Table view controller (Ref : See this i used it already)
If any solutions other than that.
Please guide me!
Thank you
alternatively you can use UITableView's tableHeaderView instead of using UITableView's viewForHeaderInSection because Section Headers WILL always stick to the top of the tableview unless otherwise if you do some simple hacks
If you make your SectionHeader opaque and not transparent/translucent you'll understand. Try making a Custom Project (test project) where you create a TableView with a lot of Sections With Different Headers
Perfect example without creating the Sample Project would be the Phone Application (the green one) in your iPhone where the Letters are Section Headers and they stick at the top of your screen when scrolling
This is an example in Swift 2.3
let someHeader = NSBundle.mainBundle().loadNibNamed("", owner:self, options nil!)!.first as! SomeView
//this is not a real function just what you wanna do stuff
someHeader.customizeIfNeeded ... yadda yadda set frame assign values add targets etc
tableView.tableHeaderView = someHeader
tableView.reloadData()
Use UIViewController instead of UITableViewControllerand add a UIView as header in top of tableView
Refer this maintain the header of a tableView fixed
Just an easy approach, hope this helps
If you have to use 'UITableViewController' look at below solution also
UITableViewController (static cells/keyboard handling) and have a fixed header

How do I set a layout margin for a text field in swift?

I am attempting to create margins in my text field so that when I go to type, the text isn't pressed so tightly against the edge.
I tried using this code (above viewDidLoad)
var insets = UIEdgeInsetsMake(10, 10, 10, 10)
Then putting this in my viewDidLoad()
textField.layoutMargins = insets
I ran the program and it still looked like there were no margins. How do I implement margins in a text field in Swift?
Subclass UITextField and implement textRectForBounds:. The simplest strategy is to call super, get the resulting rect, inset it as desired, and return it.
Here's an example result; note that the start and end of the text have considerable white space at the margin (of course the exact amount is up to you):
By creating new UIView with the right(your) values, you can set the padding in UITextField
textField.leftView = UIView(frame: CGRect(x: 30, y: 30, width: 100, height: 100))
textField.leftViewMode = UITextFieldViewMode.Always

Resources