I would like to add image and texts (under each image view) in ScrollView.
I add ScrollView firstly and add UIImage View. But, I add two images in image array and it only appears one image when I run the app. Here is my code;
#IBOutlet weak var imgView: UIImageView!
var imgArray = [#imageLiteral(resourceName: "home_image"),#imageLiteral(resourceName: "provider_image")]
and in ViewDidLoad() function,
for index in 0 ..< imgArray.count
{
let imgView = UIImageView()
debugPrint(index)
imgView.image = imgArray[index]
}
I also want to add each image caption under each image. How to do it? Why only one image appear? :(
Try this:
let width = customScroll.frame.size.width
for item in 1..<4{
image = UIImageView(frame: CGRect(x: (CGFloat(item) * width - width), y: 10, width: width, height: 250))
image.image = UIImage.init(named: "img"+String(item))
customScroll.addSubview(image)
print(image.frame)
}
customScroll.contentSize = CGSize(width: view.frame.size.width * 4, height: 300)
Related
I'd like to do what this guy was trying to do but in SwiftUI. How can I convert the code in the answers to apply it to SwiftUI?
This is what I have tried so far:
struct ImageMerger {
func merge(_ bottomImageName: String, with topImageName: String) -> UIImage {
let bottomImage = UIImage(named: bottomImageName)
let topImage = UIImage(named: topImageName)
let size = CGSize(width: 375, height: 245)
UIGraphicsBeginImageContext(size)
let areaSize = CGRect(x: 0, y: 0, width: size.width, height: size.height)
bottomImage!.draw(in: areaSize)
topImage!.draw(in: areaSize, blendMode: .normal, alpha: 1)
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}}
And to apply it to my view I did the following:
struct Test: View {
var imageMerger = ImageMerger()
var body: some View {
Image(uiImage: imageMerger.merge("Consulta-Dep", with: "CellBackground"))
}}
I switched the bottom image with the top image so you could see that the images aren't changing sizes. This is what it looks like in the Preview:
Ok, so this isn't exactly answering my question, but since one of the pictures is a blank shape, this is what ended up working for me:
Image("topImageName")
.resizable()
.scaledToFill()
.frame(width: UIScreen.main.bounds.width * 0.8)
.cornerRadius(15)
.overlay(RoundedRectangle(cornerRadius: 15)
.stroke(Color.white, lineWidth: 0))
.shadow(radius: 10)
The important part is using a cornerRadius modifier (which clips the view) and then overlay a stroke with no width.
I actually got the idea from this post.
I hope someone else can benefit from this answer!
This is my Array of some Rec I need to know if I can read size of this Rec without using GeometryReader, for example width of Rec?
var body: some View {
let arrayOfRec = [Rectangle().frame(width: 100, height: 100), Rectangle().frame(width: 200, height: 200)]
VStack
{
ForEach(0..<arrayOfRec.count) { index in
arrayOfRec[index]
}
}
}
Unfortunately, it does not seem possible to get the width and height of the Rectangle without geometry reader in pure SwiftUI. I tried the methods that were attached to that Shape, but nothing resulted in the size.
Unknown what the use case is, but it is possible to make a custom object that holds the width and height of the rectangle.
struct Rectangle_TEST {
var rectangle: Rectangle
var width: CGFloat
var height: CGFloat
init(width: CGFloat, height: CGFloat) {
self.width = width
self.height = height
self.rectangle = Rectangle()
}
func getView(color: Color) -> some View {
return self.rectangle
.fill(color)
.frame(width: self.width, height: self.height)
}
}
struct ContentView: View {
var body: some View {
let arrayOfRec: [Rectangle_TEST] = [Rectangle_TEST(width: 100, height: 100), Rectangle_TEST(width: 200, height: 200)]
VStack {
ForEach(0..<arrayOfRec.count) { index in
Text("Width: \(arrayOfRec[index].width), Height: \(arrayOfRec[index].height)")
arrayOfRec[index].getView(color: .red)
}
}
}
}
Above I made the custom object and stores the width and height that can be accessed individually, and then a function that will return a view, and you can pass in whatever modifiers are needed. But, currently, I don't think it is possible to get the dimensions without geometry reader in SwiftUI.
I have a simple test application and I want to pan an image inside its view. It will not pan or zoom and I can't see what's wrong with my code.
I have followed this tutorial but implemented it in code. I've made the image width the same as the height so I can pan without necessarily zooming.
Here is my code
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
let scrollView: UIScrollView = {
let scrollView = UIScrollView()
return scrollView
}()
let zoomImageView: UIImageView = {
let imageView = UIImageView()
imageView.isUserInteractionEnabled = true
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFill
imageView.image = #imageLiteral(resourceName: "lighthouse")
return imageView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let screenSize = UIScreen.main.bounds
let screenHeight = screenSize.height
scrollView.frame = CGRect(x:0, y:0, width: screenHeight, height: screenHeight)
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 3.0
zoomImageView.frame = CGRect(x:0, y:0, width: screenHeight, height: screenHeight)
scrollView.addSubview(self.zoomImageView)
view.addSubview(scrollView)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return zoomImageView
}
}
Search in your code for the term contentSize. You don't see it, do you? But the most fundamental fact about how a scroll view works is this: a scroll view without a contentSize cannot scroll (i.e. "pan" as you put it). In particular, it must have a content size larger than its own bounds size in order to be able to scroll along that axis (height or width, or both). Otherwise, there is nothing to scroll.
First post and I'll be honest, I'm pretty bad at coding. I'm working with Swift 3 for a school project and I have a question.
I'm creating multiple subviews through a for in loop through an array...
...and each created subview contains one programmatically created label, each with a different word, and a programmatically created UI Tap Gesture attached to each view...
Import UIKit
class ViewController: UIViewController, UIGestureRecongizerDelegate
{
var array = ["Each","One","Has","Its","Own","Name"]
var label = UILabel()
var DynamicView = UIView()
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
createSubView()
//end SuperView
}
func createSubview()
{
for thing in array {
var string = array[count]
count = count + 1
DynamicView = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 160))
DynamicView.center = CGPoint(x: xV,y: yV)
DynamicView.backgroundColor = UIColor.gray
self.view.addSubview(DynamicView)
label = UILabel(frame: CGRect(x: 25, y: 25, width: 100, height: 21))
label.center = CGPoint(x: 60, y: 13)
label.textAlignment = .center
label.font = UIFont(name: "Arial", size: 13)
label.text = "\(string)"
self.DynamicView.addSubview(label)
let tapGesture = UITapGestureRecognizer(target: self, action:
#selector(ViewController.tapped(recignizer:)))
DynamicView.isUserInteractionEnabled = true
DynamicView.addGestureRecognizer(tapGesture)
}
}
func tapped (recignizer:UITapGestureRecognizer){
print("\(label.text!)")
}
}
... (I'm excluding the code that separates all the views from being stacked on top of each other, so please assume that these views are spaced out, and this list of views is in a scroll view in my actual code, just not cramming that spaghetti code in here)...
...The result of the code is a layout of subViews, each with its own label that is a different word, and a tap gesture for each subView which triggers a function...
...What I want is a way to tap on each subview and find what the text in the label is. As of right now, when you tap on a view, you get what the label.text was in the last View made, so the result of print("label.text!") --> 'Name' , which makes some sense.
Is there any possible way to find the specific labels text even though they are all called "Label"?
I'm not looking to do a collection view or anything of the sort, I need something of this sort of coding to get what I need done.
Thank you to whoever helped this foolish swift student, sorry that my Swift lingo is hot garbage like my code.
Your problem is you are initializing the same DynamicView and label inside the for loop so it will overwrite and it will have last value from the array. To solved the problem you need to create separate UIView and separate UILabel in for loop. Also you need to set userInteraction of label to true because by default its false.
for thing in array {
let dynamicView = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 160))
dynamicView.center = CGPoint(x: xV,y: yV)
dynamicView.backgroundColor = UIColor.gray
self.view.addSubview(dynamicView)
let label = UILabel(frame: CGRect(x: 25, y: 25, width: 100, height: 21))
label.center = CGPoint(x: 60, y: 13)
label.textAlignment = .center
label.font = UIFont(name: "Arial", size: 13)
label.text = thing
self.dynamicView.addSubview(label)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapped(recignizer:)))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tapGesture)
}
Now your all view’s origin is CGPoint.zero so only last view label will visible and when you tap on label to get text of its you can get this way.
func tapped(recignizer:UITapGestureRecognizer){
if let tappedLabel = recignizer.view as? UILabel {
print("\(tappedLabel.text)”)
}
}
Ok, I am working in Swift playgrounds here and cannot figure out how to present an SkScene that is another class. So far I have placed this SKScene file, GameScene.swift, in my Sources folder and made it public.
In my main playground file I have tried:
import PlaygroundSupport
import SpriteKit
import XCPlayground
import Foundation
let frame = CGRect(x: 0, y: 0, width: 500, height: 600) //view size
let view = SKView(frame: frame)
let scene = GameScene(fileNamed: "GameScene")
//scene.backgroundColor = NSColor.blue
view.presentScene(scene)
view.showsFPS = true;
PlaygroundPage.current.liveView = view
Along with any other answer I could find on stack overflow. Nothing is working - the playground timeline is just a blank grey and this is output:
Is this impossible?
Yes it i possible, but at the moment your Gamscene object is nil. This is because GameScene(filenamed: "" ) only works when you have the scene builder. Wish your not using in Xcode.
Instead. Use
GameScene(size: CGSize)
and make sure you create the game scene with
class GameScene : SKScene {
override sceneDidLoad() {
super.sceneDidLoad()
}
}
after that present your scene from a view
let sceneSize = CGSize(width: 250, height: 550)
let scene = GameScene(size: sceneSize)
let viewRect = CGRect(x: 0, y: 0, width: 250, height: 550)
let skView = SKView(frame: viewRect)
skView.presentScene(scene)
PlaygroundPage.current.liveView = skView
Thats an example to get you started ^^