Image in ScrollView not being scrollable - swift

I was asked to help out in a project with swift and work in xcode on a short timescale, neither which I have worked with before.
The task is getting a picture to be "zoomable". And after some researched I found that putting a UIScrollView on top of the image will suffice. Now, since the image view already exists and is integrated in the code on a level I do not dare triffle with, with some constraits and what not.
I dont want to start the process all over with a new image view and later try to hook it into the code. Mostly because the image view is inside a table cell inside of a table view.
What I have done is:
On the storyboard
Put the image view inside the view on the storyboard.
Assigned the delegate of the scroll view to the table cell.
I couldnt quite figure out how to constraint these so I mostly use 'Add missing constraints'.
In code for the TableViewCell
Inherited the UIScrollViewDelegate
made a new var with #IBOutlet weak var scrollView: UIScrollView!
Since I am in a controller, i cannot override viewDidLoad function, so I implemented that and set minimum and maximumZoomScale to some values.
Implemented a viewForZooming function that returns the UIImageView
I figured out somewhat how I can use the constraints and properties of the ImageView to resize and stuff, but the regardless of what I try to do, I cannot get the "zoom" to work.
Is there any property that the ImageView could have that is messing this up, what should I check for?

After reading and trying to figure out, what finally made it work was this:
The ScrollView was made parent in the Interface Builder to the ImageView
I magically made the constraints to fit the ScrollView aligned to everything around it mostly by Add missing constraints when prompted with constraint errors.
Rightclicked the ScrollView then for the delegate, clicked the + sign and dragged that to the UITableViewCell.
I also did the same for the Referencing Outlet
And code for the UITableViewCell is this:
class ClothesItemTableViewCell: UITableViewCell, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
var theImage: UIImage? {
set {
clothinhImageView.image = newValue
} get {
return clothingImageView.image
}
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return clothingImageView
}
}
Might not be perfectly clear, and I couldnt find the exact article that made the work done. But there are plenty that tries to solve this issue in different ways, and somehow this worked. Hopes it helpes somebody!

Related

Swift Scroll down should make Bar Item disappear

I want that the Bar Button Item diappear when I scroll down. My problem is that I don't know how to detect the scroll? I have tried some code but nothing worked. For example:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("123")
}
(Don't work, I call the method in viewDidLoad())
Also I don't know where I have to call the method. In viewDidLoad(), viewDidAppear() or somewhere else? I am new in Swift, sorry.
Does anyone know the answer?
The thing is you are calling it from viewDidLoad, that gets called only once when the view is loaded. You need to place this scrollViewDidScroll
function separately, after viewDidLoad for example, but not inside of it.
Also make sure you implemented UIScrollViewDelegate in your file. Then you can add scrollViewDidScroll method to detect when is the view scrolled and print('123') inside of it.
Also make sure to set your scrollView.delegate = self in your viewDidLoad.

how to move profile picture to a navigation bar when scrolling up?

I have created this layout using UITableView header and I'm trying to move profile picture when scrolling up and removing full name and Phone number. First screen UI is what I have created and it is made in tableview header and
I want to achieve a functionality of a second screen when scrolling up.
You need add UIScrollViewDelegate to your tableView.
Table view inherit UIScrollView and their UIScrollViewDelegate methods.
So... you need set like this:
Somewhere set delegate:
tableView.delegate = self
(I think you mb already implement this because you need to implement UITableViewDelegate methods)
and implement UIScrollViewDelegate method:
extension YourControllerOrAnotherDelegateClass: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// use scrollView.contentOffset to calculate when you need to update navbar
}
}
inside this method you can get content offset of your scrollView and update nav bar when you needed.

How to add IBOutlet of a NSTextField in a custom View correctly in macOS?

I try to create an outlet from a NSTextView, which is a subview of my NSView derived custom class called GeneralView. I want to connect this outlet to my GeneralView class but Xcode doesn't like it. I get the following screen:
In this image, I want to connect an #IBOutlet from the NSTextField that contains '0000' to 'tfLiid' in GeneralView.
It only works if I create a #IBOutlet to the ViewController.
But I have like more than 50 views (textfields, progress bars, checkboxes and so on...) I need to update, so I want to split those views in 3 parent views and keep the ViewController clean by holding only the 3 parent (or group) views.
The image below makes you understand more of what I tried to achieve (I hope).
I just don't know how to get this done right in Xcode, it's not a programming problem. Thank you.
I found a solution, just by experimenting trying out. I just couldn't let it go. Anyway, you have to drag from the little #IBOutlet circle (on the line number) to the view you want to connect it to. I don't know why the other way around doesn't work, there must be a reason for it.
An image below for a visual example:

CollectionViewCell's IBoutlet is nil in Custom Keyboard Extension

Well, it'd day 5, I have tried everything to make this work. Setup is simple
I am trying to write a custom keyboard.
Which is having the CollectionView, CollectionViewCell is powered by xib
I have one label, which would hold the emoji when running.
I did ready every single article here where IBOutlet is nil.
Nothing is pointing to the direction that I haven't tried.
class CategoryCollectionCell: UICollectionViewCell {
#IBOutlet weak var emojiLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
}
Outlet is connected to the class.
KeyboardViewController is CollectionViewData source and delegate.
I am able to write the same code and UIViewController is able to get the collectionView Cells and showing the emojis.
I have run out of options. So I am posting here.
There is nothing extraordinary that I am doing here. Except its a keyboard extension.
Also, SIGQUIT is the biggest problem, any suggestions what is causing that. as I haven't able to put a finger on it.

Programmatically adding InterfaceBuilder-created views to an NSStackView squashes them to 0 height

I've been trying to add a custom view I've created in Interface Builder to an NSStackView, but am failing miserably.
This is my view controller. It currently does nothing but initialize the view from the NIB:
class ServiceControlViewController : NSViewController {
init() {
super.init(nibName: "ServiceControlView", bundle: nil)!
}
}
And here's how I'm trying to add the newly created view to the stack view (this happens in the app delegate):
#IBOutlet weak var stackView: NSStackView!
#IBAction func addButtonClicked(sender: AnyObject) {
let viewController = ServiceControlViewController()
stackView.addView(viewController.view, in: .top)
stackView.layoutSubtreeIfNeeded()
}
When I print the frame of the created view before it is added to the stack view it has the correct size. If I print it after, it is squashed to zero height. I suspect there is some auto-resizing magic going on that I don't understand, but I haven't been able to fix it.
Interestingly enough, if I set the ServiceControlViewController's view to e.g. be an NSButton, then it is correctly added and not squashed to zero height. It only happens with the Custom Views I create.
PS: My InterfaceBuilder-created view simply contains a bunch of buttons and a label:
PPS: I've added an MWE
It seems like there are no constraints on that custom view to make it larger than zero size.
You'll have to determine what its sizing behavior is, e.g. is it always the same size, or resizable with a minimum, etc.
And then in IB you can build the constraints necessary to create that effect. e.g.:
Alternatively, you could put these controls into a stack view to get a similar result.
Interestingly enough, if I set the ServiceControlViewController's view to e.g. be an NSButton, then it is correctly added and not squashed to zero height. It only happens with the Custom Views I create.
Correct. This is because an NSButton has something your custom NSViews do not have: an intrinsicContentSize. This is what the stack view uses to size and position your added views.
So, override intrinsicContentSize in your custom views and all will be well.