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.
Related
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:
In a collectionView, I'm using several textFields with different issues. All have an "extended keyboard" with custom UIBarButtons. As I need to reuse it a lot, I solved my problem with using an extension, quite similar to this one : https://stackoverflow.com/a/31010668/8162027.
My buttons are always the same: "cancel" and "Done".
But the func linked must return different things. Is there a way to override those #objc func for these 2 buttons donePressed() and cancelPressed() from my UIcollectionView?
Here is a screenshot of my buttons:
https://imgur.com/BvXrwMh
This question already has answers here:
UITextView data change swift
(6 answers)
Closed 4 years ago.
Text Editor
What I'm Doing
Text View
I am attempting to create a UITextView that is a code editor on IOS and I am trying to make different types of code be different colors so everything isn't just one color (such as comments would be grey).
Problem
The problem I am having is that I am linking the Text View like this:
#IBOutlet weak var textView: UITextView!
and I need to detect when the textView has been changed do you know of any way to do this?
To do this, you need to conform to the UITextViewDelegate protocol and then set the textView's delegate to self.
So, after UIViewController on the beginning of this view controller, you need to add the protocol conformance. Example
class MyViewController: UIViewController, UITextViewDelegate {
then set the delegate in viewDidLoad():
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
}
Now, you're set up for success. All of the delegate methods on UITextViewDelegate are optional, so you only need to implement the ones you want. In this case, the one you want to implement is func textViewDidChange(_ textView: UITextView)
That method will get triggered every single time the text in the text field updates.
Create Editing Changed #IBAction from your UITextView in storyBoard into your viewController.
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!
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.