I am trying to move a swift file that I exported from PaintCode 2 into my project. I did the following.
Exported the project via PaintCode
Moved the file into Xcode
On my storyboard I put a UIView
Clicked on the newly created UIView and selected the class that I imported from PaintCode.
Now I am looking a white box:
Now the file I got from PaintCode was of type NSObject which I knew couldn't be right so I changed it to UImage. Still getting the same result.
---------UPDATE-----------
I created a new Cococa Class of type UIView, and did the following:
#IBDesignable
class CareerButtonClass: UIView {
//CareerButton.drawCanvas2()
func drawCareerButton() {
CareerButton.drawCanvas2()
}
}
Then I named the custom class on IUView in the storyboard CareerButtonClass.
Here are what my files look like:
Still getting the same result.
For those who run by this post I finally found a good example here:
PaintCode Tutorial for Developers (Swift 2.0): Getting Started
That's because PaintCode exports a class with a collection of methods that you can use to draw the content.
Depending on the methods you choose, it can either generate a UIImage for you or you can call the code to draw in the current context.
It's up to you to implement where to use those methods.
The class you got from PaintCode is NSObject and it’s right. StyleKit is a collection of all graphics you use in your app.
To render the button, you need to override drawRect method in your UIView. And call the method to draw the Career Button.
#IBDesignable
class CareerButtonClass: UIView {
override func drawRect(rect: CGRect) {
StyleKit.drawCareerButton()
}
}
See PaintCode FAQ #29.
Related
While attempting to modify the Apple tutorial found here on IBinspectable properties for my own project I have run into a major roadblock.
I defined a simple button subclass:
import UIKit
#IBDesignable public class SocialMediaLoginButton: UIButton {
private var _loginType: SocialMediaLoginType = .facebook
#IBInspectable public var loginType: SocialMediaLoginType
= SocialMediaLoginType.facebook {
didSet {
self._loginType = loginType
}
}
}
And then added a UIButton to my storyboard which I then selected the CustomClass as the above type. When I switch over to the Attributes inspector the property is not there for me to adjust. I have no idea what is going on:
I have looked up other questions however there are either extremely outdated or have no answers on the app developer forum.
I have tried:
Manually refreshing the views
closing and reopening xcode
removing and re-adding the control
adding different controls like UIView and attempting same thing
None of these have worked. I am at a loss for how to fix this. I am using Xcode 8.3.3
Interface Builder supports the inspection of basic types (and their corresponding optionals), including: Booleans, numbers, strings, as well as CGRect, CGSize, CGPoint, and UIColor.
I think your type does not work with interface builder.
To add to Wilson answer, you MUST EXPLICITLY specify the type (Int, String, etc.) for it to show.
I have a class that defines all styles on a UIVIew.
They are all predefined but I'm not sure when to fire this.
When I try to create an extension for this:
extension UIView
{
func willMoveToSuperview(newSuperview: UIView?)
{
self.stylize() // Another extension somewhere (not here my problem)
}
}
And I'm getting this error:
Method 'willMoveToSuperview' with Objective-C selector conflicts with
previews declaration with the same Objective-c selector
I have tried to override it, but didn't worked either.
Any ideas on how to be able to apply a same behaviour when all of my UIViews will become visible?
You can use Swizzling technic to customize UIView's function. Take a look at:
http://nshipster.com/method-swizzling/ (objective-c)
or
http://nshipster.com/swift-objc-runtime/ (swift)
Hope that helps.
Even though Swift's Extensions are similar to Categories from Objective-C, what you are trying to do is not allowed in Swift.
You cannot override existing functionality:
Extensions can add new functionality to a type, but they cannot override existing functionality.
Source: Swift Extensions - Apple Documentation
Depending on what it is that you are trying to style, you might want to take a look at UIAppearance, it will allow you to style default colors for the UINavigationBar, amongst other things. NSHipster has a good post about it: NSHipster - UIAppearance
You can create a subclass of UIView with the method .stylize().
Then each view you create, you inherit of you UIView subclass.
You'll be able to cal .stylize() on each UIViewSubclass. Simply write the style code inside the subclass and inherite.
Or
Use a category to add the method to the existing UIView class.
See : https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html
Outside of swizzling (not generally recommended), or subclassing as noted by David in his answer, there isn't really a way to override existing methods on a class and its subclasses.
One thing you might try is creating a base class for your view controller instead of all your views. In your view controller base class, you could override viewWillLayoutSubviews to recurse through the view hierarchy and call stylize on each view. This means you would be using the subclass approach in fewer places (just view controllers as opposed to all views).
Another thing you might consider if taking the subclassing approach with UIView is that if you are subclassing anyway, you can take advantage of things like #IBDesignable and #IBInspectable to better integrate those UIView subclasses with storyboards and live preview.
I wrote a Swift library which does exactly this, and it works well for the type of styling it seems you want to do: https://github.com/daniel-hall/Stylish
I had two separate Xcode projects that I have fused into a single one: a speech to text recognizer that is implemented with a XIB file, written in Objective-C, and another project that is just a conventional UI written in Swift with some more stuff. Both projects work flawlessly by themselves.
I have cleaned out all the errors, and created a Bridging header file. So far, both Swift and Objective-C modules coexist, but obviously only the Swift part runs.
I want to open the Objective-C XIB and display it in my swift-coded UI.
According to your screenshot, I assume you are using storyboard for your UI in your Swift application.
As your legacy UI is on XIB files, you need to load it programatically. To do that, you'll have to use loadNibNamed method like in the following example :
var nibs = NSBundle.mainBundle().loadNibNamed("Counter", owner: self, options: nil)
for v in nibs {
if (v is Counter) {
someViewInStoryboard.addSubview(v)
}
}
Here I have a Counter.xib file which contains an custom UIView. Class for this view is Counter, which extends UIView.
So when your XIB is loaded, browse it to find your custom view, then add it as a subview of an existing UIView in your storyboard. Variable someViewInStoryboard in my example is an IBOutlet of this UIView.
i'm very new to xcode. i've googled this and even serched here and i didn't find exactly what i was looking for. i really wanna get this. especially since it's the most basic thing there is.
so, i have the .h and .m files
i want to DRAG a uiview into the .xib file, i don't want it to be as big as the screen, so i resize it.
now i want to draw a bunch of rectangles INSIDE this view object.
how do i go about doing this?
your help would be greatly appreciated!
So you add a generic UIView then create a subclass of UIView and do your drawing inside the drawRect method.
Then change the class of the UIView to your subclass name in the inspector.
Be sure to #include your header in the app delegate.
That's the basic thing.
For more sophistication, you're going to want to learn to use UIViewController subclasses as well. These are used all over ios.
I am trying to implement the OBShapedButton class in my iOS project so I can create irregular shaped buttons defined by png files. This is a link for more information on OBShapedButton.
Basically this class works great when I'm using it with my nib files. However I prefer using storyboard in my projects. But when I try to do the same thing in storyboard, following all the same steps for making an irregular button as I do in my nib files, it doesn't work and the clickable area is still defined by a rectangle and not my png image.
My question is how would I go about using storyboard with OBShapedButton? Is there something I am missing about storyboard that would prevent OBShapedButton from working?
I'm using it in a project with Storyboards. Be sure the set the Custom Class in the Identity inspector to OBShapedButton, I don't think I did anything else special.