Setting a Border for TextView (Xcode 6.1) - swift

I've tried finding the answer to this and every time it's assuming I know way too much. I'm a total beginner. I just created a new, blank application. I dragged the TextView to the storyboard. What do I do next to give it a border?
There is no code other than the default, autogenerated code at this point.

Here are the steps:
If you let Xcode create a project, go to the ViewController.swift file. Here you can create an outlet.
#IBOutlet var text : UITextField?
Now you can connect the text outlet to the textfield in the storyboard. You can do this by choosing the assistant editor. Than control drag a line from the outlet in the code to the textfield.
After the textfield is connected, you can add code to make a border in the viewDidLoad function.
class ViewController: UIViewController {
#IBOutlet var text : UITextField?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
text!.layer.borderWidth = 1
text!.layer.borderColor = UIColor.redColor().CGColor
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

In Swift 3 and Xcode 10,
tested and working in (swift 5 and code 12)
first, create an outlet of your text view
And In your viewdidload put these two lines
class ViewController: UIViewController {
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
self.textView.layer.borderColor = UIColor.lightGray.cgColor
self.textView.layer.borderWidth = 1
}
}

Related

Using a slider to transition between 2 images

I am trying to learn the basics, but still very new to Xcode and Swift. I want to use a slider to transition between two images. When the slider is all the way to the left, image 1 should show in the image view. As the slider moves to the right, image 1 should fade and an image 2 should come into view. At the halfway point, both images should be visible (alpha = 0.5 for both image), superimposed one on top of the other. As the slider goes all the way to the right, image 1 should vanish and image 2 should be the only one visible.
Using UIImageView and a slider, I am able to adjust the alpha and "fade" my image based on the position of the slider.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var UAimage: UIImageView!
#IBOutlet weak var Slider: UISlider!
#IBAction func sliderValueChange(_ sender: UISlider) {
var currentValue = Float(sender.value)
UAimage.alpha = CGFloat(sender.value)
}
}
At this point, I am looking for help getting the second image added and using alpha to programmatically control its opacity.
Just add a 2nd imageview over the first and add this line
imageview2.alpha = CGFloat(1 - sender.value)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var UAimage: UIImageView!
/*
Your 2nd image.
On storyboard, go place a UIImageView directly on top of your initial imageView and set the image to whatever you'd like just as you did the original.
*/
#IBOutlet weak var UAimage2: UIImageView! //Your 2nd image
#IBOutlet weak var Slider: UISlider!
#IBAction func sliderValueChange(_ sender: UISlider) {
var currentValue = Float(sender.value)
UAimage.alpha = CGFloat(sender.value)
UAimag2.alpha = 1 - CGFloat(sender.value)
}
}

how to set background colour of a container view

i have dragged a container view from storyboard and set it black in background colour. but it didn't change the background colour.
class ViewController: UIViewController {
#IBOutlet weak var container: UIView!
override func viewDidLoad() {
super.viewDidLoad()
container.layer.backgroundColor = UIColor.blackColor().CGColor
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
How did you set the background color?
A container view is actually just a normal view, that is linked to a viewController. This means you can set the background the same way as you would for any other NSView or UIView. This is all the code I needed to add to my NSViewController class (not the ViewController inside the container, just the ViewController for the window).
#IBOutlet weak var containerView: NSView!
override func viewDidLoad() {
//other code in your viewDidLoad
containerView.wantsLayer = true
}
override func awakeFromNib() {
super.awakeFromNib()
containerView.layer?.backgroundColor = NSColor.black.cgColor
}
make sure to connect the IBOutlet if you have not already.
If you set the layers background color in viewDidLoad, the layer may not exist yet, (I don't know why). Accessing the layer in awakeFromNib has always worked for me, while accessing it in the viewDidLoad can be unreliable.
If you are working on IOS, most of this is not applicable, and this should be all you need
#IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
containerView.layer?.backgroundColor = UIColor.black.cgColor
}
I work less on IOS, so I have not ran into any issues with that, but that could be from lack of attempts. On OSX setting the background color in viewDidLoad will work about 50% of the time so there might still be an issue that I have not ran into.
If neither works, try unwrapping the layer rather than leaving it an optional (replacing the ? with a !) this will at least crash your program and probably tell you that layer is nil, if this is the case you should be figuring out why the layer is nil.
Also if the ViewController connected to the container view is a custom class, you don't have to bother with the IBOutlets, just call the view "view" in that custom class.
Sorry this got a bit long, but hope this helped

viewWillAppear method doesn't work

import UIKit
class ViewController: UIViewController {
#IBOutlet weak var family_name: UITextField!
#IBOutlet weak var given_name: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(animated: Bool) {
family_name.center.x -= view.bounds.width
given_name.center.x -= view.bounds.width
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
viewWillAppear has no effect. When I run my app both text fields shouldn't be visible, but they are. I attached a photo with constraints; I think this can be the problem!
You are using autolayout, so frame change will not apply. your constraints will overwrite frame values after viewWillAppear:
Create IBOutlet for either right constraint or width constraint of your textFields.
Check below sample image to create IBOutlet of constraint.
In above image I have create outlet of height constraint.
To send textField outside of the view, suppose you have created right constraint.
//suppose initial value of right constraint is 20
constraintTextView1Right.constant = 20 + self.view.bounds.width;
self.view.layoutIfNeeded();//forcefully apply constraint with new values
After this, at some point in button click or in other event or after some delay, you can change value of constraintTextView1Right to 20,
constraintTextView1Right.constant = 20
self.view.layoutIfNeeded();//forcefully apply constraint with new values
your textFiled will come in the screen.
To animate the same you can write below code.
constraintTextView1Right.constant = 20
UIView.animateWithDuration(0.3) { () -> Void in
self.view.layoutIfNeeded();//forcefully apply constraint with new values
}

Scroll Position of UITextView at the top

I am using swift and want a UITextView to be at the top when the view launches. At the moment when I launch the app the UITextView is scrolled to the end. I have tried looking online and think scrollRangeToVisible might work but do not know how to use it in swift.
import UIKit
class ThirdViewController: UIViewController {
#IBOutlet weak var FunFact: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
FunFact.scrollRangeToVisible(0, 0)
// Do any additional setup after loading the view.
}
}
Add this to your ViewController:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
textView.layoutIfNeeded()
textView.contentOffset = CGPoint.zero
}
This scrolls the UITextView to the top. Since it only knows its size, when it is layouted, "layoutIfNeeded" is needed after you changed the text (i changed it in viewDidLoad).
Try this:
var zeroOffset = CGPoint.zeroPoint
FunFact.contentOffset(zeroOffset)
This should bring the offset to 0 (the offset is an indication of how far the current position is from the initial one)

Swift: Scroll view with image view isn't enabled?

So I created a scroll view and put an image view in it. I initialized the scroll view as a variable and wrote the following code in my ViewController and yet, the image won't scroll.
What's wrong?
Later on I'm also going to need to put several buttons into this scroll view so if I'm missing something here, it'd be great if you could address it for buttons as well.
My code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
ScrollView.scrollEnabled = true
}
#IBOutlet weak var ScrollView: UIScrollView!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Try explicitly setting the content size like so:
self.scrollView.contentSize = CGSize(width:somethingBigger, height: somethingBigger);
To answer your follow-up question...Make height of the scrollView equal to that of the screen. Then it won't scroll vertically. Programmatic stuff overwrites storyboard stuff.