Try to create a scrollView with labels - swift

I'm a little new in swift. I'm trying to make a simple scrollview with a label that changes its value, but I can not get it to work properly.
This is my code:
class ConsejosController: UIViewController, UIScrollViewDelegate {
var asunto = ["value1", "value2"]
#IBOutlet weak var lbl_asunto: UILabel!
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var pageControl: UIPageControl!
var contentWidth: CGFloat = 0.0
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
for etiqueta in 0...1{
contentWidth += view.frame.width
lbl_asunto.text = asunto[etiqueta]
}
scrollView.contentSize = CGSize(width: contentWidth, height: view.frame.height)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
pageControl.currentPage = Int(scrollView.contentOffset.x / CGFloat(375) )
}
}
This don't work properly. Directly show "value2" and don't scroll
Any advice?

I made the for cycle in this way:
for etiqueta in 0...1{
contentWidth += view.frame.width
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .center
label.text = asunto[etiqueta]
self.scrollView.addSubview(label)
}
Now scrolling well, but show two labels 'value1' and 'value2' in the same page one over another, and the second page empty.

Related

Change default possition of view in Xcode

I'm trying to change a default position of my view for a user.
First I tried it with constraint like this:
class ShopSummaryViewController: BaseViewController, OverlayViewController {
// MARK: properties
#IBOutlet weak var heightConstraint: NSLayoutConstraint!
let overlaySize: CGSize? = CGSize(width: UIScreen.main.bounds.width,
height: UIScreen.main.bounds.height)
#IBOutlet weak var trayView: UIView!
override func viewDidLoad() {
let onePercent = UIScreen.main.bounds.height / 100
heightConstraint.constant = onePercent * 4
}
which work, but for my purpose of UIGestureRecognizer it would be better to not use constraint, but something like this
#IBOutlet weak var heightConstraint: NSLayoutConstraint!
let overlaySize: CGSize? = CGSize(width: UIScreen.main.bounds.width,
height: UIScreen.main.bounds.height)
#IBOutlet weak var trayView: UIView!
override func viewDidLoad() {
trayView.center = CGPoint(x: trayView.frame.size.width/2, y: trayView.frame.size.height/2)
}
which doesn't work. Could someone help me move the default position of the view by around 50% down

UIScrollview height won't change programatically in swift

Try to change scrollview height, but it doesn't work programatically
#IBOutlet weak var containerView : UIScrollView!
public override func viewDidAppear(_ animated: Bool) {
self.containerView.frame = CGRect(x: 0, y: 0, width: containerView.frame.width, height: UIScreen.main.bounds.height / 2)
}

I cannot set background picture (Swift)

I made a simple timer app and I want to add a background picture or video in the background. However, I cannot do it and I do not know why.
Can someone tell me why the picture does not show up in the background? I set the background in the viewDidLoad method. Maybe the #IBOutlet var backgroundView: UIView! part is wrong? I connected it with the given UIView when I made this Xcode project.
class ViewController: UIViewController, UITableViewDelegate {
let mainStopwatch = Stopwatch()
let lapStopwatch = Stopwatch()
var isPlay: Bool = false
var laps: [String] = []
//UI components
#IBOutlet weak var lapRestButton: UIButton!
#IBOutlet weak var playPauseButton: UIButton!
#IBOutlet weak var lapTimerLabel: UILabel!
#IBOutlet weak var timerLabel: UILabel!
#IBOutlet weak var lapsTableView: UITableView!
#IBOutlet var backgroundView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.addBackground(name: "nathan-dumlao-iXXyrEwZgiU-unsplash")
lapRestButton.isEnabled = false
lapsTableView.dataSource = self
lapsTableView.delegate = self
}
//UI Settings
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
//Actions
#IBAction func playPauseTimer(_ sender: Any) {
lapRestButton.isEnabled = true
changeButton(lapRestButton, title: "Lap", color: .black)
}
}
And this is another swift file for setting the background picture.
extension UIView {
func addBackground(name: String) {
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageViewBackground.image = UIImage(named: name)
imageViewBackground.contentMode = .scaleAspectFill
self.addSubview(imageViewBackground)
self.sendSubviewToBack(imageViewBackground)
}
var timer = Timer()
var counter = 0 // the counter is use for the index of array in below code
var imageArr = ["image","image1","image2"] //Image arr
override func viewDidLoad() {
super.viewDidLoad()
self.addBackground(name: "image") // set default image on your view
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) // timer will start and it will change view background image after one min you can change time according to your requirement.
}
// called every time interval from the timer
#objc func timerAction() {
self.addBackground(name: self.imageArr[counter]) // here we are change our view background image when timer function is run on every min
counter += 1 // every-time counter will increment by one so the image array index will also increment and we will get our new image
}
// that's your code only you need to change one line that last one
func addBackground(name: String) {
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageViewBackground.image = UIImage(named: name)
imageViewBackground.contentMode = .scaleAspectFit
self.viewref.addSubview(imageViewBackground) // here we will set the image view to our view
}
hope this will help you for change your background of view.
Thanks.
Seems like you've don't using your backgroundView to anywhere in class code , So first
Remove backgroundView from storyBoard and also remove code from class file
viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
self.addBackground(name: "nathan-dumlao-iXXyrEwZgiU-unsplash") //Update this line
lapRestButton.isEnabled = false
lapsTableView.dataSource = self
lapsTableView.delegate = self
}
Update your extension code to below one
extension YourViewController {
func addBackground(name: String) {
let width = self.view.frame.size.width
let height = self.view.frame.size.height
let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageViewBackground.image = UIImage(named: name)
imageViewBackground.contentMode = .scaleAspectFill
self.view.addSubview(imageViewBackground)
// self.sendSubviewToBack(imageViewBackground) //Comment this line
}
}

Dynamic View that adjust to how long the Label text Swift

What I want to happen is that the rectangle view will adjust to how long the label is. Here are my codes
#IBOutlet weak var rectangleView: UIView!
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let stringNumber = "123ewrqrqwerw4"
let numberFromString = Int(stringNumber)
rectangleView.frame = CGRect(x: 15, y: 100, width: self.view.frame.width * numberFromString, height: 50)
self.label.text = stringNumber
}
but my code is wrong.

Event scrolling (scrollViewDidScroll) never called - Swift 3

I would like to detect when the user scroll, I used a UIScrollView, I implemented the UIScrollViewDelegate in my ViewController and tried scrollViewDidScroll, scrollViewDidEndScrollingAnimation and all the others but these events are never called.
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let xOrigin = self.view.frame.width
let view1 = View1(frame: CGRect(x: 0, y: 0, width:self.view.frame.width, height: self.view.frame.height))
let view2 = View2(frame: CGRect(x: xOrigin, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let view3 = View3(frame: CGRect(x: xOrigin * 2, y: 0, width: self.view.frame.width, height: self.view.frame.height))
scrollView.addSubview(view1)
scrollView.addSubview(view2)
scrollView.addSubview(view3)
self.scrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.height)
// hide the scrol bar.
scrollView?.showsHorizontalScrollIndicator = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("end scroll")
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
print("end scroll")
}
}
You just have to add the delegate to your viewDidLoad() func:
override func viewDidLoad() {
super.viewDidLoad()
//add this line
self.scrollView.delegate = self
}