Draw custom view within UITextField - ios

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)

Related

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.

Swift 3 - Sliding stack views?

I am currently in process of making a registration form on my app in Swift 3 / xcode and I first have 3 text fields showing with a continue button underneath, I want it so the user presses the continue button and the current stackview slides out of view and a new one slides in (similar to a segue with the storyboards
This will show first and then they press continue:
Then this stack view will show:
Could somebody please point me in the right direction of how to do this?
Thank you.
For your case, I'm not pretty sure of what is the purpose of using a stackView. I suggest to use UICollectionView (horizontal) instead (UIStackView does not scroll which is inappropriate for what are you trying to achieve), especially that the two views should be the same cell in your case. All you have to do is to check what's the current indexPath.row for determining what cell's components should look/behave.
Also, You can add target to the button to let the collectionView scrolls to the next cell if it's the first row, for the second row it should -for example- submit the form.
Hope it helped.
#Chad, if you want to use scrollable and easy solution you could try ScrollableStackView library. Here is the github page :
https://github.com/gurhub/ScrollableStackView
Here is some sample codes :
Swift
import ScrollableStackView
var scrollable = ScrollableStackView(frame: view.frame)
view.addSubview(scrollable)
// add your views with
let rectangle = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 55))
rectangle.backgroundColor = UIColor.blue
scrollable.stackView.addArrangedSubview(rectangle)
// ...
Objective-C
#import ScrollableStackView
ScrollableStackView *scrollable = [[ScrollableStackView alloc] initWithFrame:self.view.frame];
scrollable.stackView.distribution = UIStackViewDistributionFillProportionally;
scrollable.stackView.alignment = UIStackViewAlignmentCenter;
scrollable.stackView.axis = UILayoutConstraintAxisVertical;
[self.view addSubview:scrollable];
UIView *rectangle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 55)];
[rectangle setBackgroundColor:[UIColor blueColor]];
// add your views with
[scrollable.stackView addArrangedSubview:rectangle];
// ...
Hope it helps.

Showing a label right below navigation bar in iOS

How do I show a label (or any UIView) right below navigation bar in a UIViewController.
I used the following code:
let label = UILabel(frame: CGRectMake(100, (self.navigationController?.navigationBar.frame.height)!, 200, 21))
However, using self.navigationController?.navigationBar.frame.height)! is not sufficient. Using this y co-ordinate keeps label invisible.
What should I use for y co-ordinate to show a UIView right below navigation bar.
Try to:
let label = UILabel(frame: CGRectMake(100, (self.navigationController?.navigationBar.frame.height)!, 200, 21))
y = self.navigationController?.navigationBar.frame.origin.y + self.navigationController?.navigationBar.frame.size.height;
after set y value.
Also consider the status bar height which can be found using
UIApplication.sharedApplication().statusBarFrame.size.height
so it should be
UILabel(frame: CGRectMake(100, ((self.navigationController?.navigationBar.frame.height)! + UIApplication.sharedApplication().statusBarFrame.size.height), 200, 21))

How to fix the UIImage overlapping in UIAlertController?

I'm using this code:
// Declare a UIImageView, set its frame and add an image to it
var myImageView = UIImageView(frame: CGRectMake(0, 0, 100, 100))
myImageView.image = UIImageView(image:UIImage(named:"myImage"))
// Let's add the UIImageView to the alertController
alertController.view.addSubview(myImageView)
However, the view is overlapping the UIAlertController:
How can I fix it?
You've no business adding subview to an alert view. It is easy to make a view-controlled presented view that behaves like an alert view, and that's what you should do. That way, you are in charge of the view and you won't have any problems laying it out in a nib / storyboard with autolayout or whatever.

How to make UITextField appear with Swift Code

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).

Resources