Is it possible to make #IBDesignable override Interface Builder values? - xcode

For example, I want to subclass UIButton and set it's font to 20.0f by default. I can write something like this:
#IBDesignable
class HCButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.customInit()
}
func customInit () {
titleLabel?.font = UIFont.systemFontOfSize(20)
}
}
But this does not affect preview in Interface Builder, all custom buttons appear with 15.0f font size by default. Any thoughts?

I have created new IBInspectable as testFonts :
import UIKit
#IBDesignable
class CustomButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
self.customInit()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.customInit()
}
func customInit () {
titleLabel?.font = UIFont.systemFontOfSize(20)
}
convenience init() {
self.init(frame:CGRectZero)
self.customInit()
}
override func awakeFromNib() {
super.awakeFromNib()
self.customInit()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.customInit()
}
}
Hope it helps you :)
This is working for me.

I think you must override the init with frame initializer as well to affect that
override init(frame: CGRect) {
super.init(frame: frame)
self.customInit()
}

EASIER: The following solution usually works or me
import UIKit
#IBDesignable
class CustomButton: UIButton {
open override func layoutSubviews() {
super.layoutSubviews()
customInit()
}
func customInit () {
titleLabel?.font = UIFont.systemFontOfSize(20)
}
}
I hope that's what you're looking for.

Related

How to enable copy paste in my NStableviewcell in my cocoa application programatically

Below is my code for my NSTableview cell
class MyTableCellView: NSTableCellView {
var aTextField: NSTextField?
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
aTextField = NSTextField(frame: frameRect)
aTextField?.drawsBackground = false
aTextField?.isBordered = false
self.addSubview(aTextField!)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
How can I enable copy and paste to this cell programatically?

NSCollectionView (osx 10.11) hangs when running

I've made a subclass of NSCollectionView which conforms to NSCollectionViewDataSource, NSCollectionViewDelegate but it hangs everytime - I only get the spinning beach ball.
public class SequenceCollectionView : NSCollectionView, NSCollectionViewDataSource, NSCollectionViewDelegate {
// MARK: Inits
public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
self.dataSource = self
self.delegate = self
}
//MARK: Datasource
public func collectionView(collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
public func collectionView(collectionView: NSCollectionView, itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem {
if indexPath.item == 0 {
let item = NSCollectionViewItem(nibName: "ItemView", bundle: nil)
}
return NSCollectionViewItem()
}
}
It's obviously in some kind of loop or retain cycle but I'm finding it hard to debug.
It transpires that the issue lies with the optional delegate call
public func collectionView(collectionView: NSCollectionView, willDisplayItem item: NSCollectionViewItem, forRepresentedObjectAtIndexPath indexPath: NSIndexPath) {
}
By adding this to the class has fixed the issue.
Adding to Chris' answer, the NSCollectionView will hang on scrolling or resizing the view unless the following optional delegate method is overriden:
- (void)collectionView:(NSCollectionView *)collectionView didEndDisplayingItem:(NSCollectionViewItem *)item forRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {}

IBDesignable can't set background on buttons

I'm trying to create a custom button in my IOS project. But when I'm creating the button with the my class the background doesn't work in the storyboard. But when I run the program the button works fine.
Here you see my class
#IBDesignable class GreenButtonTest : UIButton{
override init(frame: CGRect) {
super.init(frame: frame)
style()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
style()
}
private func style() {
self.backgroundColor = UIColor(CGColor: "#6eb18c".CGColor)
}
}
You need to override prepareForInterfaceBuilder() and call style() from there too.
Try this:
#IBDesignable class GreenButtonTest : UIButton{
override init(frame: CGRect) {
super.init(frame: frame)
style()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
style()
}
override func prepareForInterfaceBuilder() {
style()
}
private func style() {
self.backgroundColor = UIColor.greenColor()
}
}

Custom UILabel-Subclass does not appear in dropdown selection of IB

I wrote a tiny class that inherits from UILabel
import UIKit
class ConnectionStateLabel: UILabel, BTConnectionStateChangedDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
func didConnect() {
self.text = "Connected"
}
func didDisconnect() {
self.text = "Disconnected"
}
}
As far as I know, in Xcode that class should now be selectable from the dropdown menu (section "Custom Class") for any UILabel that I drag into my story board, but it does not show up. What is wrong with that class? What did I miss?

UISegmentedControl selected Segment always -1

I subclassed UISegmentedControl in order to trigger the valueChanged method which works fine. Now when I select a segment the index is always -1.
I am on Xcode 6 beta 4, I can't find what I am doing wrong.
The subclass:
import UIKit
class MySegCotrl: UISegmentedControl {
init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
let current = self.selectedSegmentIndex
super.touchesBegan(touches, withEvent: event)
println("Current:\(current)")
println("Self.selectedSegmentIndex:\(self.selectedSegmentIndex)")
if current == self.selectedSegmentIndex {
self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
}
}
}
The valueChanged method in my UIViewController class
#IBOutlet var segCotrRelevanz: MySegCotrl!
#IBAction func relevanChanged(sender: AnyObject) {
println("sender.selectedSegmentIndex:\(sender.selectedSegmentIndex)")
println("segCotrRelevanz.selectedSegmentIndex:\(segCotrRelevanz.selectedSegmentIndex)")
if sender.selectedSegmentIndex == segCotrRelevanz.selectedSegmentIndex {
println("Same")
}
}
The output:
Current:-1
Self.selectedSegmentIndex:-1
sender.selectedSegmentIndex:-1
segCotrRelevanz.selectedSegmentIndex:-1
Same
Try change method to touchesBegan not touchesEnded, left all as it should be:
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
let current = self.selectedSegmentIndex
super.touchesBegan(touches, withEvent: event)
println("Current:\(current)")
println("Self.selectedSegmentIndex:\(self.selectedSegmentIndex)")
if current == self.selectedSegmentIndex {
self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
}
}

Resources