Set of UIImageView.Image re-positions parent view - swift

I have a UIView and added a UIImageView as child to it. I later change the position of the UIView via another UIButton event.
If I change the image of the UIImageView the UIView - inc. all shields - moves back to the initial position.
Could anyone maybe explain me what is going on here :-D How do I keep the new position of the UIView ?
Here a image screen I took to visualize the issue
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var number: UIImageView!
#IBOutlet weak var popup: UIView!
#IBAction func openPopup(sender: UIButton) {
self.popup.center = sender.center
//old position: (190.5, 124.0) - new Position: (173.0, 414.0)
}
#IBAction func updateImage(sender: UIButton) {
self.number.image = UIImage(named: "img-1")
}
}
Update:
I found out that above code works if I deactivate autolayout. Here is a solution to do this with auto layout:
Auto Layout center UIView above another UIView at its center

Are you sure of that? Did you check by logging popup.frame.origin after updateImage being called? Have you properly set clipsToBounds and contentMode properties for number uiimageview?

I found out that above code works if I deactivate "Use Auto Layout" in Interface Builder Document section at tab File Inspector.
In addition here is a working solution to the issue using auto layout and constraints:
Auto Layout center UIView above another UIView at its center

Related

Touch Gesture on image is not working in ScrollView? - Swift

This is the image on the scroll view where I'm able create those rectangles from superview
This is the image I want how to look my output when I touch to began creating multiple rectangles
I'm trying to create multiple annotations on images using views where it's been achieved. And for the next step trying to zoom in through image view to annotate where it's possible with pinch gesture but the constraint is not able scroll left or right while zoomed in. I have to zoom out and then again have to zoom in to the concentrated area or co-ordinate to annotate again. I gone through few examples and found to be possible of moving or scrolling left or right while zoomed in using scroll view, where I have achieved it. But not able to annotate the image by creating views. When I tried to create it's not happening still accidently I srolled it from superview where it has created the view. Thank you in adavnce.
Class ViewController : UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate{
// Outlets
#IBOutlet weak var displayImageView : UIImageView!
#IBOutlet weak var imageScrolling : UIScrollView!
#IBOutlet weak var containerView : UIView!
// View Did Load
override func viewDidLoad() {
super.viewDidLoad()
displayImageView.isMultipleTouchEnabled [](https://i.stack.imgur.com/ba2n7.png)= false
displayImageView.isUserInteractionEnabled = true
imageScrolling.minimumZoomScale = 1.0
imageScrolling.maximumZoomScale = 4.0
imageScrolling.delegate = self
imageScrolling.contentSize = displayImageView.bounds.size
imageScrolling.isUserInteractionEnabled = true
containerView.isUserInteractionEnabled = true
imageScrolling.isExclusiveTouch = true
imageScrolling.panGestureRecognizer.isEnabled = true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
// View For Zooming - Scroll View Image Zooming and Scrolling
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return displayImageView
}
}
I have also added the screenshot of how it's happening while touch began from superview and how i wanted it to be.
I have also added views hierarchy

No scrollbars appear when adding a custom view via addSubview to NSScrollView

I dynamically want to add a custom NSView as a content of a NSScrollView via code in runtime.
So I added a NSScrollView to my NSWindow, created an outlet to this NSScrollView and add my custom view.
#IBOutlet var myScrollView: NSView!
#IBOutlet var myCustomView: NSView!
myScrollView.addSubview(myCustomView)
This works fine (the content of myCustomView is shown in the NSScrollView) but the NSScrollView scrollbars are disabled and the content (that is larger than the NSScrollView) cannot be scrolled.
Is there something I am missing?
Thanks!
To add scrollable content to a NSScrollView you should set documentView and not via addSubview(_:)
let myView = MyView(frame: <somerect>)
scrollView.documentView = myView
If scrolling is still not working you may need to specify contentSize
Docs for NSScrollView

Making UIScrollView with an UIImageView scale content automatically using Swift, autolayout and SnapKit

I have a view controller set up in Interface Builder with the following view structure:
- UIView
* UIScrollView
* UIImageView
I want to assign an image to UIImageView so, that the scroll view (and it's content size) will adopt the same width than in the image and automatically calculate the height of the content size according to the image aspect ratio. (This makes the image vertically scrollable inside the UIScrollView.)
I'm using Swift and Interface Builder + SnapKit for managing autolayout constraints.
I managed to make the constraints using SnapKit in the following way:
In Interface Builder, select the view controller and create all missing constraints by selecting Editor / Resolve Auto Layout Issues / Add Missing Constraints
Manually select all generated constraints and select Placeholder / Remove at build time from Attributes Inspector
Assign UIScrollView and UIImageView outlets to the view controller
Assign an image to the UIImageView
Then implement the view controller as follows:
import UIKit
import SnapKit
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.snp.makeConstraints { (make) in
make.edges.equalTo(view)
}
let ratio = imageView.image!.size.height / imageView.image!.size.width
imageView.snp.makeConstraints { (make) in
make.top.equalTo(0.0)
make.bottom.equalTo(scrollView.snp.bottom)
make.width.equalTo(view.snp.width)
make.height.equalTo(scrollView.snp.width).multipliedBy(ratio)
}
}
}

(swift) update a number to label will make the view get back to original position

demo image
When I update a number to label will make the view get back to original position.
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
#IBOutlet var aview: UIView!
var number = 0
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func plus(sender: UIButton) {
number++
label.text = "number:\(number)"
}
#IBAction func move(sender: AnyObject) {
aview.frame.origin.y -= 20
}
}
I couldn't find the answer on web, please help me to fix this problem.Thank you very much!
Because your xib or Storyboard you set use Autolayout:
So if you don't set constrain system will auto generate it. When you change frame by set frame it effect but when you access to it. It will auto back to old position.
if you don't want it happen. You set in viewDidLoad:
self.view.translatesAutoresizingMaskIntoConstraints = false
Issue is probably related to a constraint reloading when the label reloads.
Instead of setting aview.frame.origin.y -= 20 you should make an outlet to the constraint holding the y position of your aView and then update the constant of that constraint outlet instead.

Swift - Cropping images *outside* of allowsEditing

I have a very very simple project set up that allows you to click a "browse photo" button. The user then selects a photo from their photo gallery, and it's displayed on a programmatically created UIImageView.
Works like a charm. However - I am missing key functionality that is required.
I need the user to be able to scale the image (via pinching and dragging) after it is displayed within the UIImageView. allowsEditing = true, lets the user crop before. I need similar functionality, however, allowing them to edit once it's on the main UI.
Help is appreciated. Please and thank you!!
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
var imageViewLayer: CALayer{
return imageView.layer
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imageViewLayer.contents = UIImage(named: "ss3.jpg")?.CGImage
imageViewLayer.contentsGravity = kCAGravityResizeAspect
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func newGesture(sender: AnyObject) {
imageViewLayer.transform = CATransform3DMakeScale(sender.scale, sender.scale, 1)
}
}
I did something similar a while back. I added the image to UIImageView's layer property, added gesture recognizer to the view and implemented the gesture call backs modifying the layer property and not the view. Adding the image to the UIImageView's layer did the trick. As a side note, I would like to add that every UIView is supported by CALayer class. It has a lot of methods and properties which help to dynamically change the view, which in your case will be done by gestures.
As an alternative, you can also use CALayer's hitTest method instead of implementing the call backs for gesture recognizers.
EDIT- Sample Code
You could do some thing like this:
#IBOutlet weak var imageView: UIImageView!
var imageViewLayer: CALayer{
return imageView.layer
}
In the viewDidLoad, set up the image
imageViewLayer.contents = UIImage(named: "CoreDataDemoApp")?.CGImage
imageViewLayer.contentsGravity = kCAGravityResizeAspect
Add pinch gesture to the imageview in storyboard (or programmatically) and in it's call back you could do something like this:
#IBAction func pinchGestureRecognized(sender: AnyObject) {
imageViewLayer.transform = CATransform3DMakeScale(sender.scale, sender.scale, 1)
}
Again this is just to give you an idea of how it could work and it is not the complete code. Hope this helps!
This is another way of doing it:
Stackoverflow link to related question