Updating Scrolling of a static tableView - ios

I am trying to get the scrollview of a uitableview that is static to get updated, I have a container view that will change sizes, and that works, but i cannot get the scrollable area to update.
I have tried setNeedsDisplay and setNeedsLayout and no luck.
Any ideas?
class TimeAwayRequestTableViewController: UITableViewController {
var originalHeight : CGFloat!
#IBOutlet var selectedDate : UIView!
#IBOutlet var calendarView: CalendarView!
override func viewDidLoad() {
super.viewDidLoad()
originalHeight = self.tableView.contentSize.height
calendarView.delegate = self
self.selectedDate.frame.size.height = CGFloat(SelectedDatesTimeAway.selectedDates.count * 44)
}
override func preferredContentSizeDidChangeForChildContentContainer(container: UIContentContainer) {
self.selectedDate.frame.size.height = CGFloat(SelectedDatesTimeAway.selectedDates.count * 44)
self.tableView.contentSize.height = self.originalHeight + self.selectedDate.frame.size.height
self.tableView.setNeedsDisplay()
self.tableView.setNeedsLayout()
}
}
extension TimeAwayRequestTableViewController : CalendarViewDelegate {
func calendarDidSelectDate(date: Moment) {
let theDate = date.date
SelectedDatesTimeAway.selectedDates.append(theDate)
print(SelectedDatesTimeAway.selectedDates.count)
let tbc = self.childViewControllers[0] as! UITableViewController
tbc.preferredContentSize.height = CGFloat ( SelectedDatesTimeAway.selectedDates.count * 44 )
print(tbc.preferredContentSize.height)
tbc.tableView.reloadData()
}
func calendarDidPageToDate(date: Moment) {
print(date)
}
}

If I understand the nature of your problem, you need to update the contentSize property of your UIScrollView when you change the content.

Related

Pinch to Zoom UIImageView

I have an image view as a subview of a UIScrollView in order to enable zooming the image. It somewhat works but not well.
class LargeImageViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
#IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
#IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
#IBOutlet weak var imageViewTrailingConstraint: NSLayoutConstraint!
var selectedImage: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
if let image = selectedImage {
imageView.image = image
}
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
updateMinZoomScaleForSize(view.bounds.size)
}
func updateMinZoomScaleForSize(_ size: CGSize) {
let widthScale = size.width / imageView.bounds.width
let heightScale = size.height / imageView.bounds.height
let minScale = min(widthScale, heightScale)
scrollView.minimumZoomScale = minScale
scrollView.zoomScale = minScale
}
#IBAction func doneTapped(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
extension LargeImageViewController: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
updateConstraintsForSize(view.bounds.size)
}
func updateConstraintsForSize(_ size: CGSize) {
let yOffset = max(0, (size.height - imageView.frame.height) / 2)
imageViewTopConstraint.constant = yOffset
imageViewBottomConstraint.constant = yOffset
let xOffset = max(0, (size.width - imageView.frame.width) / 2)
imageViewLeadingConstraint.constant = xOffset
imageViewTrailingConstraint.constant = xOffset
view.layoutIfNeeded()
}
}
Any ideas?
I have now tried following the tutorial suggested and it behaves a little better, but the image is zoomed in and huge.
My constraints are still giving me issues even though I followed the tutorial.
Couple reasons your code is not working like the tutorial.
1 - You missed setting the scroll view delegate (unless you set it in Storyboard). If you did not set it in Storyboard:
override func viewDidLoad() {
super.viewDidLoad()
if let image = selectedImage {
imageView.image = image
}
// add this
scrollView.delegate = self
}
2 - It will still not be quite correct, because the tutorial sets the image in Storyboard, but you're setting it in viewDidLoad(). To fix that:
// remove this
//override func viewWillLayoutSubviews() {
// super.viewWillLayoutSubviews()
// updateMinZoomScaleForSize(view.bounds.size)
//}
// add this
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
updateMinZoomScaleForSize(scrollView.bounds.size)
updateConstraintsForSize(scrollView.bounds.size)
}
3 - To get rid of the constraint errors in your Storyboard, give the image view Width and Height constraints (such as 100 each), and set them as Placeholders so they will not be used at run-time:
Can't say for sure where the problem is since you've probably configured the storyboard/xib file as well.
However, I recommend you to go through the guide: https://www.raywenderlich.com/5758454-uiscrollview-tutorial-getting-started.

How to do zoom in, out in UIScrollView using UISlider?

Mainscroll is scrollview which was added into another internal UIView and i have UISlider to change internal view scale.
But, MainScroll.setZoomScale(CGFloat(sender.value), animated: true) this line is not work.
==> Here is my code.
override func viewDidLoad() {
super.viewDidLoad()
MainScroll.maximumZoomScale = 1
MainScroll.minimumZoomScale = 10
MainScroll.delegate = self
MainScroll.zoomScale = 5
}
#IBAction func Scrolling(_ sender: UISlider) {
MainScroll.setZoomScale(CGFloat(sender.value), animated: true)
}
Here is a minimal example for getting a UIScrollView to zoom.
The key is that you have to implement func viewForZooming(in scrollView: UIScrollView) -> UIView? and return a view (that is inside your scrollView) that is to be scaled (think of it like your 'document' or 'canvas' view).
Also, notice that zoom is working for the default pinch gestures on the scrollView as well.
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
// Assume that outlets exist on a storyboard
#IBOutlet var scrollView: UIScrollView!
#IBOutlet var canvas: UIView! // A view that is added in the scrollView
#IBOutlet var slider: UISlider!
let minScale: CGFloat = 1
let maxScale: CGFloat = 10
let initialScale: CGFloat = 1
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
scrollView.minimumZoomScale = minScale
scrollView.maximumZoomScale = maxScale
scrollView.zoomScale = initialScale
slider.minimumValue = Float(minScale)
slider.maximumValue = Float(maxScale)
slider.value = Float(initialScale)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return canvas
}
#IBAction func sliderValueChanged(_ sender: UISlider) {
scrollView.zoomScale = CGFloat(sender.value)
}
}

horizontally scaling a background color image

I am new to iOS and Swift. I am trying to add a background image for a slide right effect. I want that image to stick to the right of the screen no matter what orientation. The image will always be a set size (now its (382x145px). How can I adjust it on landscape orientation so that the picture stays to the right.
Here is my custom view cell class.
class CustomRouteViewCell: UITableViewCell {
#IBOutlet var downImage: UIImageView!
#IBOutlet var downTime: UILabel!
#IBOutlet weak var leadingSpaceConstraint: NSLayoutConstraint!
#IBOutlet weak var trailingSpaceConstraint: NSLayoutConstraint!
#IBOutlet var locationTitle: UILabel!
#IBOutlet weak var panView: UIView!
#IBOutlet var timerLabel: UILabel!
var indexRow: Int = 0
var timeInterval: Int = 0
override func awakeFromNib() {
super.awakeFromNib()
self.selectedBackgroundView = nil
var img = UIImage(named: "tableSwipeArrow.png")!
self.panView.backgroundColor = UIColor(patternImage: img)
if self.respondsToSelector(Selector("setLayoutMargins:")) {
layoutMargins = UIEdgeInsetsZero
}
if self.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
preservesSuperviewLayoutMargins = false
}
var panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
panGestureRecognizer.delegate = self
addGestureRecognizer(panGestureRecognizer)
}
func handlePanGesture(recognizer: UIPanGestureRecognizer) {
switch(recognizer.state) {
case UIGestureRecognizerState.Changed:
var translation = recognizer.translationInView(recognizer.view!)
var little = CGFloat(trailingSpaceConstraint.constant + translation.x * -1)
trailingSpaceConstraint.constant = fmax(0, little)
leadingSpaceConstraint.constant = fmin(0, leadingSpaceConstraint.constant + translation.x)
recognizer.setTranslation(CGPointZero, inView: recognizer.view!)
timeInterval = Int(trailingSpaceConstraint.constant / 30) * 5
timerLabel.text! = "\(timeInterval)"
case UIGestureRecognizerState.Ended, UIGestureRecognizerState.Cancelled:
var refreshAlert = Alarm.getReminderView(self.timeInterval, view: self.parentViewController!.view)
self.parentViewController?.presentViewController(refreshAlert, animated: true, completion: nil)
leadingSpaceConstraint.constant = 0
trailingSpaceConstraint.constant = 0
UIView.animateWithDuration(0.25, animations: { () -> Void in
self.layoutIfNeeded()
})
default:
trailingSpaceConstraint.constant = 0
leadingSpaceConstraint.constant = 0
}
}
}
extension CustomRouteViewCell: UIGestureRecognizerDelegate
{
override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer.isKindOfClass(UIPanGestureRecognizer) {
var velocity = (gestureRecognizer as! UIPanGestureRecognizer).velocityInView(gestureRecognizer.view!)
return fabs(velocity.x) > fabs(velocity.y)
}
return true;
}
}
Use auto layout to link the position of the images.
In your case you can connect the trailing of the background image with the container view.
Hold Control on image and link the Trailing Attribute with the main view
Just adjust the constant to 0.

UIScrollView cannot display image either in Portrait or Landscape mode

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
}
}

Subview of UIScrollView don't resize (manually)

I have a UIScrollView with a UIView inside of it. When I try to manually resize the UIView by code, it doesn't work. I tried to set the autoResizingMask property of the UIView to .None and I implemented the touchesShouldCancelInContentView method of the UIScrollView to return true but it still doesn't work. Here's my code :
class ViewController: UIViewController {
#IBOutlet var scrollView: CustomScrollView!
#IBOutlet var field: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
var gesture = UITapGestureRecognizer(target: self, action: "gestureHandler")
gesture.numberOfTapsRequired = 1
view.addGestureRecognizer(gesture)
scrollView.contentSize = CGSizeMake(430, 140)
field.autoresizingMask = .None
}
func gestureHandler() {
field.frame.size.width += 20
var view = UIView(frame: CGRectMake(0, 0, 30, 100))
view.backgroundColor = UIColor.redColor()
field.frame.size.width -= 30
field.frame.origin.x += 30
scrollView.addSubview(view)
}
}
class CustomScrollView: UIScrollView {
override func touchesShouldCancelInContentView(view: UIView!) -> Bool {
return true
}
}
What I get :
What I want :
Thanks for your help !

Resources