Swift: stop image inside scroll from moving up and down - ios

I am working on a sample to display images inside scroll. The image is loading fine but then it allows me to drag the image up and down inside the scroll. I would still want to be able to move right and left (horizontally), but the image should be fixed from moving vertically.
The scroll view and the image constraints are set to the top, bottom, left and right as those of the main view.
What am I doing wrong?
This is my code and storyboard settings
import UIKit
class Image2ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.scrollView.contentSize = (imageView.image?.size)!
}
}
and these are my storyboard settings for ScrollView and ImageView. The top 2 are for the scroll and other 2 are for the image.

Adding this line in the viewDidload solved the issue:
self.automaticallyAdjustsScrollViewInsets = false

Related

View over tableView

I have the following screen (image) which shows a side menu when the button is pressed the menu appears, plus I have a uitableview to show other things
my problem is that when the side menu appears the uitableview is resized.
How do I keep my uitableview intact?
I've been trying the next but I have not got anything:
#IBOutlet weak var Vmenu_usuario: UIView!
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
view.sendSubview(toBack:tableView)
view.bringSubview(toFront:Vmenu_usuario)
}
If you want to delete White space view over tableview
uncheck the option 'Adjust Scroll View Insets'.
more detail: Empty white space above UITableView inside a UIView
Hope this hellp.

Scrollview just show my last array's element in view which i create with storyboard in swift 3?

I want to add a few view in scrollview. But I dont want to this view programmatically. I want to create with storyboard. This view include an image. I added scrollview and added to view. But in scrollview I just showing my last view. I want to 2 view and 2 image like my view. How can i fix this?
This is my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var mainScrollView: UIScrollView!
var imageArray = [UIImage]()
#IBOutlet weak var myView: UIView!
#IBOutlet weak var imgv: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageArray = ["image","image2"]
for i in 0..<imageArray.count{
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
myView.layer.frame.size.width = myView.frame.width * CGFloat(i + 1)
imgv.image = imageArray[i]
mainScrollView.addSubview(myView)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
This is storyboard and controller looks like
You are having an issue with constraints from what I see and that is why you might not see every element that you put on your storyboard. You need to correct those errors for everything to show up correctly in your scrollview. Keep in mind, you need to tell the scrollview what its height and width should be in the storyboard. They need to be defined in some way. This is a common mistake when working with scrollviews.
You can put a view in your scrollview and define its height and width property. You can set its width to be equal to the screen's width and you can set a constant for your height.

How do I display a UIImageView via a Switch on state? Then...how do I add constraints to the UIImageView to either center it or such?

I am trying to have a UIImageView (centered horizontally in the viewController) with a UISwitch inside of it.
When the UIswitch is turned to the ON position - a second UIImageView should appear to the left of the original UIImageView, while the constraints should make it so that both UIImageViews are centered horizontally.
thanks in advance
There are many ways to do this, but the easiest is to simply use a centered horizontal UIStackView that contains only a single imageView. When the switch is flipped add the second imageView to the stackView by calling insertArrangedSubview(:at:) and the stackView will take care of maintaining the centering and any requested spacing. Similarly when the switch is off just call removeArrangedSubview(:) and everything goes back into place.
If you are not on iOS 9 and don't have UIStackView you can just drag an IBOutlet to the view that you want to move's centerX constraint and add to its .constant property and animate layoutIfNeeded to have it slide to the right.
class ViewController: UIViewController{
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var imageViewCenterXConstraint: NSLayoutConstraint!
let padding = CGFloat(10)
#IBAction func tapButton(_ sender: Any) {
imageViewCenterXConstraint.constant = imageView.frame.size.width / 2 + padding
UIView.animate(withDuration: 0.25) { self.view.layoutIfNeeded()}
}
}

Center a UIImageView over a MKMapView

I have a view that contains both a MKMapView and an UIImageView. The MKMapView is positioned via constraints set on its auto-alignment. For the image view, I would like to programmatically position the center of the image over the center of the image view. Here is what I've tried:
class MyViewController: UIViewController {
#IBOutlet weak var mapView: MKMapView!
#IBOutlet weak var centerPinImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
centerPinImageView.bounds = mapView.bounds
centerPinImageView.center = mapView.center
centerPinImageView.frame = mapView.frame
centerPinImageView.setNeedsDisplay()
}
}
This code does not change the position of the image view. How can I fix this?
Try same code in below method instead of viewDidLoad()
override func viewDidLayoutSubviews() {
}
OR
Add this line for image view,
centerPinImageView.setTranslatesAutoresizingMaskIntoConstraints(true)
Hope it might help you.
If you are using a XIB file why don't you just make sure the MapView and ImageView are siblings and then set centre constraints for the ImageView to match those of the MapView?

Swift ScrollView In UITableViewCell not scrolling

I am new to swift and am trying to add a UIScrollview to a custom UITableViewCell. I can't seem to get the scrollview to scroll in the simulator though. I have tested both vertical and horizontal scrolling.
Here is the code for my custom UITableViewCell
import UIKit
class CustomTableViewCell: UITableViewCell, UIScrollViewDelegate {
#IBOutlet weak var winLoseValueLabel: UILabel!
#IBOutlet weak var dateLabel: UILabel!
#IBOutlet weak var newTotalLabel: UILabel!
#IBOutlet weak var locationLabel: UILabel!
#IBOutlet weak var playersLabel: UILabel!
#IBOutlet weak var playersScrollView: UIScrollView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.playersLabel.text = nil
//self.playersScrollView.contentSize.height = 1000
self.playersScrollView.contentSize.width = 1000
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func addLabelToScrollView(str : String) {
if self.playersLabel.text == nil{
self.playersLabel.text = str
}
else{
self.playersLabel?.text = self.playersLabel.text! + "\n\(str)"
}
}
}
My playersScrollView outlet has been properly connected to the scrollview in my storyboard. As far as I can see from all of the tutorials and sources online, I should just have to set the contentSize width or height (larger than the view area) to get it to scroll, which I have done in the awakeFromNib function.
Any ideas why this is not working in the simulator? Secondly, ideally I would like to have a vertical scrolling scrollview in the table cell, but I thought that the table view's vertical scrolling might have been interfering with the scrollview, so I changed it for horizontal scrolling for testing, which also didn't work.
Can you have a vertical scrollview in a vertically scrolling UITableView, or will the table view scrolling interfere too much with the scrollview?
Ok, it turns out that the constraints on my playersLabel, which is a subview of playersScrollView, was the culprit, although I am not sure why. I adjusted the constraints on the label and that allowed the horizontal scrolling to work.
The vertical scrolling still did not work, so I had to override the touchesBegan and touchesEnded methods in the scroll view so I could disable/re-enable the scrolling of the table view.

Resources