I would like to have a label that is displayed upside down, when the view did load.
Here is the default code given in the ViewController.swift, and I had also added the UILabel as an outlet.
import UIKit
class ViewController: UIViewController {
#IBOutlet var Label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Also sorry if this is a dumb question I am very new to IOS development and swift.
To rotate your label in viewDidLoad just use the following :
Swift
override func viewDidLoad() {
super.viewDidLoad()
var aLabel: UILabel = ...
aLabel.transform = CGAffineTransformMakeRotation(M_PI)
}
Objective C
-(void) viewDidLoad {
[super viewDidLoad];
UILabel *aLabel = ...
aLabel.transform = CGAffineTransformMakeRotation(M_PI);
}
M_PI will make a rotation of 180 degrees which will result in an upside down label.
Swift 3x, Xcode 8x
override func viewDidLoad() {
super.viewDidLoad()
var aLabel: UILabel = ...
aLabel.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI))
}
Xcode 8.3.3
override func viewDidLoad() {
super.viewDidLoad()
var aLabel: UILabel = ...
aLabel.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
}
Related
How can i convert view to landscape inside portrait device orientation
My View controller Code.
import UIKit
class SampleViewController: UIViewController {
var commingtextview:String = ""
#IBOutlet weak var textview: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
navigationController!.navigationBar.barTintColor = UIColorFromRGBs(0x000000)
navigationController!.navigationBar.tintColor = UIColor.whiteColor();
textview.text = self.commingtextview
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Important ! My project portrait must be portrait i want to change only this view orientation to landscape.
Also my project orientation
[x] Portrait
[ ] Upside Down
[ ] Landscape Left
[ ] Landscape Right
Use :-
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
// Set whatever frame you want
// self.view.frame = CGRectMake(xOrigin,yOrigin,Width,Height)
}
I am playing with ProgressView. What I want to achieve is stopping at 33%, 66%, 100% checkpoints on button click. It's working okay if I use progressView.progress = 0.33, but it directly lands to the checkpoint. Instead, having it smooth and accelerating would look so nice.
I thought animateWithDuration would work but unfortunately it doesn't. After some reading, I found out that I can do something with NSTimer, but I couldn't achieve it.
var progressView: UIProgressView?
override func viewDidLoad()
{
super.viewDidLoad()
progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Default)
progressView?.center = self.view.center
progressView?.trackTintColor = UIColor.redColor()
progressView?.progressTintColor = UIColor.greenColor()
view.addSubview(progressView!)
progressView!.progress = 0.0
}
Using this answer, I achieved making it move once in every second, but how can I make it go smooth, slow in the beginning and accelerating, so looks nice.
using setProgress method and setting animation to true makes animation work.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var pb: UIProgressView!
#IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pb.progress = 0.00
}
#IBAction func btnPress(sender: UIButton) {
UIView.animateWithDuration(3, animations: {
self.pb.setProgress((self.pb.progress + 0.33), animated: true)
}, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I have created a VC with orientation in Landscape in storyboard
I have added an UIIScrollView in it , say, make it: (w)1000, 500 (h) in the VC.
What I wanted to do:
1) Scrolling the image (with high resolution like 1334 x 750) inside ScrollView
2) view the image in ScrollView in landscape mode
To make ScrollView to display the image, I have to do it in `viewDidAppear`
but here the Problems:
1) The Width and height of the `ScrollView` is gone
2) label on top gone.
3) The `ScrollView` Size become small something like 200 x 150 and start from the Top corner like (0,0)
What I need to do to make `scrollview` size like before 1000 x 500?
--- Update --
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var myUIScrollView: UIScrollView!
var imgView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
//-- force to landscape mode:
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
self.myUIScrollView.maximumZoomScale = 10.0
self.myUIScrollView.minimumZoomScale = 1.0
self.myUIScrollView.delegate = self
imgView = UIImageView(image: UIImage(named: "MyPhoto.png"))
}
override func viewDidAppear(animated: Bool) {
self.myUIScrollView.contentSize = imgView.bounds.size
self.myUIScrollView.addSubview(imgView)
view.addSubview(myUIScollView)
}
override func shouldAutorotate() -> Bool {
return true
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imgView
}
Try this:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var image: UIImage!{
get{
return myImageView.image!
}set{
myImageView.image = newValue
myImageView.sizeToFit()
myScrollView.contentSize = myImageView.frame.size
}
}
var myImageView = UIImageView()
#IBOutlet weak var myScrollView: UIScrollView!{
didSet{
myScrollView.delegate = self
myScrollView.minimumZoomScale = 10.0
myScrollView.maximumZoomScale = 1.0
myScrollView.addSubview(myImageView)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
image = UIImage(named: "one")
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return myImageView
}
}
I have a problem with my app, look at this pic
Now I'm typing on textView , I want a way to resize the textview or move it to top or scrolling to the last where I'm typing please help
I saw many answers here but all not working with me , and this is the code when i tried one of the answers
import UIKit
class AddEditClassNoteViewController: UIViewController {
#IBOutlet var keyboardHeightLayoutConstraint: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
#IBOutlet weak var noteTextBox: UITextView!
override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
self.noteTextBox.resignFirstResponder()
}
func textFieldDidBeginEditing(textField: UITextField) {
animateViewMoving(true,moveValue: 100)
}
func textFieldDidEndEditing(textField: UITextField) {
animateViewMoving(false,moveValue: 100)
}
func animateViewMoving (up:Bool, moveValue :CGFloat){
var movementDuration:NSTimeInterval = 0.3
var movement:CGFloat = ( up ? -moveValue : moveValue)
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}
}
Im using Swift IOS 8
It seems like you're asking for several things, (all of which, I'm guessing, already have answers on Stack Overflow) but I'll do my best to give you a few examples.
First, what you have so far seems to hide the keyboard when you tap on the view. That may be partially defeating your purpose.
Second, if you're trying to scroll to the top of the your noteTextBox, try using:
noteTextBox.setContentOffset(CGPointMake(0, 0), animated: true) [Use animted: true if you want a scrolling animation. You may not, I don't know]
Third, check out this question for resizing the UITextView: Click Here
Fourth, check out this question for returning to a "remembered" position on your UIView: Click Here
Good luck,
Kyle
for any one need the answer with Swift language , The answer is as following simple change only .
import UIKit
class AddEditClassNoteViewController: UIViewController {
var keyboardShowing = false
#IBOutlet weak var noteTextBox: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardHide:", name: UIKeyboardWillHideNotification, object: nil)
// Do any additional setup after loading the view.
}
override func shouldAutorotate() -> Bool {
return !self.keyboardShowing
}
// much simpler than in iOS 7; a lot of the touchy bugs are gone in iOS 8
// as long as you play your part (adjust content offset),
// iOS 8 will play its part (scroll cursor to visible)
// and we don't have to animate
func keyboardShow(n:NSNotification) {
self.keyboardShowing = true
let d = n.userInfo!
var r = (d[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue()
r = self.noteTextBox.convertRect(r, fromView:nil)
self.noteTextBox.contentInset.bottom = r.size.height
self.noteTextBox.scrollIndicatorInsets.bottom = r.size.height
}
func keyboardHide(n:NSNotification) {
self.keyboardShowing = false
self.noteTextBox.contentInset = UIEdgeInsetsZero
self.noteTextBox.scrollIndicatorInsets = UIEdgeInsetsZero
}
func doDone(sender:AnyObject) {
self.view.endEditing(false)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
self.noteTextBox.resignFirstResponder()
}
}
Thanks for all who was trying to helping me .
I'm trying to make a scroll view on iOS with Swift and Autolayout, but the screen crop the content instead of resizing it.
My layout and constraints
How can i fix this ?
EDIT : Tried with UITextView it doesn't work too
EDIT 2 : I'm trying to make a screen like that and my problem is that i can't implements this screen for all screens resolution and rotation with AutoLayout
I found a solution !
Use Interface Builder only to add a UIScrollView and add constraints to fit superview, and create all subviews programatically.
Override updateViewConstraints()and for each view in the ScrollView set translatesAutoresizingMaskIntoConstraints to false and add a width constraint to keep scrollview width. Dont forget to call super.updateViewConstraint().
Override didRotateFromInterfaceOrientation() and viewDidAppear() and set scrollView.contentSize.height to wrap all items (if you don't do that, you can't scroll).
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
var constraintsUpdated = false;
var label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
label.numberOfLines = 0
label.textColor = UIColor.blackColor()
scrollView.addSubview(label)
}
override func updateViewConstraints() {
if(constraintsUpdated){
//Rotate, just update view and keep constraints
super.updateViewConstraints()
scrollView.layoutSubviews()
return;
}
var viewsDictionary = ["label": label, "scrollView": scrollView]
let screenWidth = self.view.frame.width
label.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(String(format: "H:[label(==scrollView)]", screenWidth), options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary))
constraintsUpdated = true
super.updateViewConstraints()
//Update subviews
scrollView.layoutSubviews()
}
override func viewDidAppear(animated: Bool) {
scrollView.contentSize.height = label.bounds.height
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
scrollView.contentSize.height = label.bounds.height
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I found another solution with FLKAutoLayout library, that's work great with less code!
class ViewController: UIViewController {
var labelTitle: UILabel = UILabel()
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
//TITLE
scrollView.addSubview(labelTitle)
labelTitle.textColor = UIColor.blackColor()
labelTitle.numberOfLines = 0
//constraints
labelTitle.constrainWidthToView(self.view, predicate: nil)
labelTitle.numberOfLines = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
scrollView.contentSize.height = labelTitle.frame.height
}
}
I don't know if it's a UIScrollView bug with AutoLayout or a feature