0

I have drawer controller presenting menu in the iOS app. This menu is toggled by pressing menu buttons (UIButton) available on each screen.

enter image description here

As you can see in the mock: menu buttons can have red dot showing that new content is available - for this case I simply have two images for menu button without dot and with it.

enter image description here

I thought about making custom UIControl with "global" property for this dot. Is it the right way?

class MenuButton : UIButton {
  static var showNotificationDot : Bool = false
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
moonvader
  • 19,761
  • 18
  • 67
  • 116
  • You can set different images for different states of UIButton. e.g. when you have new content you can set button's property isHighlighted = true and it will automatically change the image. – Sahil Manchanda Apr 28 '18 at 08:37
  • And how should I notify all button instances to change state? – moonvader Apr 28 '18 at 08:45
  • you can fire a custom notification whenever new content is available and the class which have this button can subscribe to listen for this notification thus change the state when you receive the notificaiton – Sahil Manchanda Apr 28 '18 at 08:49
  • so I should add notification observers to all MenuButton instances? – moonvader Apr 28 '18 at 09:50

1 Answers1

1

For example you could create subclass UIButton and add observer.

class MyButton: UIButton {

    static let notificationKey = NSNotification.Name(rawValue: "MyButtonNotificationKey")

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.subcribeForChangingState()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    fileprivate func subcribeForChangingState() {
        NotificationCenter.default.addObserver(forName: MyButton.notificationKey, object: nil, queue: nil) { notificaton in
            if let state = notificaton.object as? Bool {
                self.changeState(active: state)
            }
        }
    }

    fileprivate func changeState(active: Bool) {
        //change ui of all instances
        print(active)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

And change UI from any place like this:

NotificationCenter.default.post(name: MyButton.notificationKey, object: true)