-2

I would like to add a Like \ Favorite Button . Once you will click on the favorite button the image will change to a full red heart and place a Badge ( When Star\Heart Pressed) and if you click again the full red heart will change to empty heart .( Before Favorite Button Pressed\Pressed On and Off )

Just like in the Pictures

But i recently joined Xcode and has no idea on how to do this .... if anyone can guide me through the process that will be a life saver ! thanks a lot

Newbie Questions
  • 463
  • 1
  • 11
  • 31
  • Honestly: just google for "tutorials button swift" and *work* through the examples. If you then run into problems, come back here. Nobody's going to hold your hand until you show a willingness to do some real work. – Elise van Looij Aug 26 '16 at 11:46
  • @ElisevanLooij All the tutorials i find are outdated or not about the topic ... did you find any useful updated tutorial? – Newbie Questions Aug 30 '16 at 20:23
  • 1
    Nope. I learned how to make buttons a long time ago by working through the examples. In my time it was Objective-C and the Fahrenheit / Celsius converter. I never wanted to make a Fahrenheit / Celsius converter but what I learned there I used as the basis to make what I did want to make. – Elise van Looij Aug 31 '16 at 16:53

2 Answers2

0

Actually this is a broad questions, But I'll explain little bit about this concept because you are a newcomer.

Simple way :-

First create a button on storyboard or programmatically. As an example take a heart image. One with filled red heart and empty heart. First you need to initialize a variable which indicate whether the button is selected or not. So then you should change the image in viewDidLoad() according to the user selection. You should create a button action to change the button image when user click on that button. So when user click on the button check the initialized variable value then change to filled heartimage or empty heart image.

Here are some answer which you can use for change button image :-

How to change UIButton image in Swift

How to change UIButton image after clicking in Swift?

Community
  • 1
  • 1
Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58
0

There are 3 things to know here:

Set target to your button

mybutton.addTarget(self, action: #selector(YourViewController.handleClickAction(), forControlEvents:.TouchUpInside)

Then set button image programatically and also set badge.

func handleClickAction() {
    // set image
    let image = UIImage(named: "your-image-name") as UIImage?
    mybutton.setImage(image, forState: .Normal)

    // set badge to tabbar item. example at tabbar at index 2
    let tabItem = self.tabBarController?.tabBar.items![1]
    tabItem.badgeValue = "1"
}

Then same thing you want to do to clear the badge

tabItem.badgeValue = nil

Reference:

Set badge to tabbar

Set button action and change image

Community
  • 1
  • 1
xmhafiz
  • 3,482
  • 1
  • 18
  • 26
  • Hey thats cool thanks ! Im currently just testing and learning in a test tabbed application project and if say i want to move the badge or the page to a ehole different new favorites tab for example what would it be like – Newbie Questions Aug 23 '16 at 06:52
  • you could use persistent data http://stackoverflow.com/questions/38474564/shared-variable-between-two-tabs-for-xcode/38477145#38477145 – xmhafiz Aug 23 '16 at 07:21