0

I would like to create a feature that looks a like an accordion using objective-C. I have multiple buttons that are created dynamically. When a user clicks any of the buttons i would like to show the nib file underneath the button that was clicked. Note that only one button can be clicked at a time and show the nib underneath it.

How can i achieve this in objective-C code :

This is the sudo code of what i am trying to achieve

button.click {

show nib bottom of button;

}
kaddie
  • 233
  • 1
  • 5
  • 27

1 Answers1

0

You can place views over each other with iOS development. Let's say, for example, you have 2 UIViews that have the exact same frame. These two views let's say have a frame of x: 0, y: 0, width: 100, height: 100, so they're both squares starting from the top left of the screen. Nibs are basically customized UIViews, while a button subclasses UIView.

If you do :

[self.view addSubview:nib];
[self.view addSubview:button];

This will add the button on top of the nib, and since their frames are the same, they will be directly over each other.

If you want to ensure the button is on top of the view you could do:

[self.view bringSubviewToFront:button]; 

This aspect can also be handled directly from the storyboard.

but this is a tad unnecessary.

Now, for the button clicking up and down (I'm assuming you want to show the nib on click, and then show the button again when you release your finger), what you can do is add 2 IBAction outlets from your story board, or you can create the button actions programmatically like on this link.

One of these actions should be specified as a UIControlEventTouchDown and another as a UIControlEventTouchUpInside/Outside. In the button press/release functions, you need to simply hide the button from the overall view. This can be done through:

[button.setHidden:YES]; //On Click
[button.setHidden:NO];  //On Release
Alan
  • 1,132
  • 7
  • 15
  • i am still struggling to get it to work since i am still new to iOS – kaddie Nov 27 '17 at 12:54
  • Okay :) I tried to not give too much away in the answer so that you can try to do it yourself, let me know how it goes – Alan Nov 27 '17 at 12:57