How to make normal content look scrollable (like a UITableView) - ios

I have a view controller with a few labels and switches inside of it. Is there any way to make the whole view look "scrollable" like how UITableViewCells look. Like even if there isn't enough content to need it to be scrollable I want that interactive gesture that UITableViewCells have. The whole point of this is to make all the pages on my app feel similar. Is there any way to add that same "scrolling" or "dynamic" feeling to the normal view?
Edit: A link to show what I mean
https://imgur.com/a/wJtfIKK
Here is the code where I connect the scroll view and I am making it "scrollable"
class ThemesViewController: UIViewController {
#IBOutlet weak var spacer: UIView!
#IBOutlet weak var overrideThemeDesc: UILabel!
#IBOutlet weak var overrideSystemTheme: UILabel!
#IBOutlet weak var overrideThemeToggle: UISwitch!
#IBOutlet weak var backgroundView: UIView!
#IBOutlet weak var lightButton: RadioButton!
#IBOutlet weak var darkButton: RadioButton!
#IBOutlet weak var lightText: UILabel!
#IBOutlet weak var darkText: UILabel!
#IBOutlet weak var appearanceLabel: UILabel!
#IBOutlet var interactiveView: UIScrollView!
let themeOverrideDefaults = UserDefaults.standard
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
interactiveView.alwaysBounceVertical = true
// Rest of My Code
}
And here is a picture of the storyboard with the scroll view selected (to show it has custom class UIScrollView): https://imgur.com/a/YCKPKpj

You can embed your view into a UIScrollView and then set
scrollView.alwaysBounceVertical = true
so that you get that scrolling effect even if the content is not large enough to be scrolled.
https://developer.apple.com/documentation/uikit/uiscrollview/1619383-alwaysbouncevertical

Related

How can I add make an array for all of my IBOutlets?

These are my Outlets, how can I out all off these in an array?
#IBOutlet weak var progressBar1: UIProgressView!
#IBOutlet weak var progressBar2: UIProgressView!
#IBOutlet weak var progressBar3: UIProgressView!
#IBOutlet weak var progressBar4: UIProgressView!
Open the Assistant Editor, right-click and drag from one of your UIProgressView's or just drag from its "Referencing Outlet Collections" to the code file.
Insert outlet collection
Then you can drag from your swift file's #IBOutlet to the rest of your UIProgressView's. Add view to collection
On top declare a variable first like this
var outlets: [UIProgressView] = []
and now on ViewDidLoad method you can use this to put all outlets on that array
like this:
outlets = [progressBar1, progressBar2, progressBar3, progressBar4]
Hope you understand.
class ViewController: UIViewController {
#IBOutlet weak var p1: UIProgressView!
#IBOutlet weak var p2: UIProgressView!
#IBOutlet weak var p3: UIProgressView!
#IBOutlet weak var p4: UIProgressView!
var outlets: [UIProgressView] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
outlets = [
p1,p2,p3,p4
]
}
}
If you have other types of views you can use
var outlets: [UIView] = [...]
As mentioned here Swift - IBOutletCollection equivalent you can use IBOutletCollection to do that. You can drag all your views to one IBOutlet array.
#IBOutlet weak var progressBars: [UIProgressView]!
For example, you can access the first progressBar like
progressBars[0]
But you have to careful about the order of progressBars, when you define IBOutletCollections the collection will not be order guaranteed. You can define the for each view and sort by their tags in runtime as mentioned here also Is IBOutletCollection guaranteed to be of correct order?
To order all views by their tags like
progressBars = progressBars.sorted { $0.tag < $1.tag }

Put multiple UILabels into an array

In my app I currently have 9 labels on my storyboard, each showing a different value. (The values are stored in an array). As far as I know, each label has to be connected from the storyboard to the viewcontroller file separately, which makes my code look like this:
#IBOutlet weak var xValue: UILabel!
#IBOutlet weak var yValue: UILabel!
#IBOutlet weak var zValue: UILabel!
#IBOutlet weak var maxXValue: UILabel!
#IBOutlet weak var maxYValue: UILabel!
#IBOutlet weak var maxZValue: UILabel!
#IBOutlet weak var minXValue: UILabel!
#IBOutlet weak var minYValue: UILabel!
#IBOutlet weak var minZValue: UILabel!
And to set the values, I need to manually do:
xValue.text = arr[0]
yValue.text = arr[1]
...
minYValue = arr[7]
minZValue = arr[8]
Is there a way to connect multiple labels from the storyboard into an array so that I can simply do something like:
for i in 0...8 {
labelArray[i] = arr[i]
}
As rmaddy mentioned in a comment you can use an outlet collection:
#IBOutlet private var labels: [UILabel]!
Then in your storyboard labels will show up under Outlet Collections when right-clicking your ViewController, and you can link multiple labels:
You can put all the UILabel into an array, like this:
let labelArray = [xValue, yValue, zValue]
for i in 0..<labelArray.count-1{
labelArray[i] = arr[i]
}

Changing the image of a button based on control state

I am trying to change the image of a button when clicked, they are stars like a rating system. when clicked the image remains the same and does not change when clicked. I set the default image in story board.
class DetailViewController: UIViewController {
#IBOutlet weak var detailTitleViewLbl: UILabel!
#IBOutlet weak var detailDescription: UILabel!
#IBOutlet weak var yearLabel: UILabel!
#IBOutlet weak var star1: UIButton!
#IBOutlet weak var star2: UIButton!
#IBOutlet weak var star3: UIButton!
#IBOutlet weak var star4: UIButton!
#IBOutlet weak var star5: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func star1Pressed() {
star1.setImage(#imageLiteral(resourceName: "favorite (1).png"), for: UIControlState.selected)
}
func star2Pressed() {}
func star3pressed() {}
func star4pressed() {}
}
A button doesn't change to the selected state when clicked. You need to write code that sets the button's selected flag to true when it's clicked.
Note that you can connect more than one IBAction to a button, so you could have one action that sets the selected flag to true, and another that does custom "stuff", so you don't have to duplicate the "boilerplate" code to set the selected state in the IBAction of every such button.

animate not work in the case of using autolayout

When I use this func,the animate not worked.
#IBOutlet weak var containerHeight: NSLayoutConstraint!
#IBOutlet weak var scanTopConstraint: NSLayoutConstraint!
#IBOutlet weak var scanView: UIImageView!
private func starAnimate() {
scanTopConstraint.constant = -containerHeight.constant
scanView.layoutIfNeeded() //if I delete this code, the animate will work,but it is not I want to effect
UIView.animateWithDuration(1.5) {
self.scanTopConstraint.constant = self. containerHeight.constant
UIView.setAnimationRepeatCount(MAXFLOAT)
self.scanView.layoutIfNeeded()
}
}
With this code, scanView just show the last status.
Could someone tell me why.
Thank for your reading and any help will be appreciated.

Collection of Collection of UIViews

In my Swift app I have 9 outlets:
#IBOutlet weak var day1: UIImageView!
#IBOutlet weak var day1Title: UILabel!
#IBOutlet weak var day1Description: UILabel!
#IBOutlet weak var day2: UIImageView!
#IBOutlet weak var day2Title: UILabel!
#IBOutlet weak var day2Description: UILabel!
#IBOutlet weak var day3: UIImageView!
#IBOutlet weak var day3Title: UILabel!
#IBOutlet weak var day3Description: UILabel!
I'd like to "group" these together by their day using an integer as the key. So 1 maps to day1, day1Title, day1Description.
So that I could assign label text to each of the above generically rather than have to reference the specific image view, label, and description.
Any thoughts/suggestions?
I thought maybe:
Dictionary<int, Array<UIImageView, UILabel, UILabel>>
or
Dictionary<int, Array<UIView>>
but i'm not quite sure about this being the right move. Could I somehow assign a class that has 3 properties that reference these outlets maybe?
Or perhaps 3 outlet collections?
What do you think is good to do?
Thanks!
Why use a dictionary with an integer key? An array of 9 elements should work just as well. In addition, the value would be better represented as a tuple instead of as an array.
Array<(UIImageView, UILabel, UILabel)>
or
[(UIImageView, UILabel, UILabel)]
result
var views: [(UIImageView, UILabel, UILabel)]
…
views = [(day1, day1Title, day1Description),
(day2, day2Title, day2Description),
…
(day9, day9Title, day9Description)]
…
var (imageView, titleLabel, descriptionLabel) = views[dayNumber - 1]

Resources