Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Alexander Certosimo
1,774 PointsIf i have an imageview behind a textview, can i make the textview disappear if an image is inserted ?
Hello all,
I have an odd question, but curious if there is a solution. I have two textviews that i use to get user inputted data. I also want to add in an imageview, but i do not want to change the layout of the view. Is there any way to put the imageview behind the textview and if an image is chosen, make that textview disappear and then show the image in its place?
Any help or idea would be appreciated. Thank you
1 Answer
Steven Deutsch
21,046 PointsHey Alexander Certosimo,
You can manipulate the hidden properties of your textview and image views. You can click on these elements in the storyboard, then go to the attribute inspector, and check the hidden box to disable them by default. If you want to make them reappear in your code, just set the @IBOutlet's hidden property to true.
//Example
@IBOutlet weak var textLabel: UILabel!
// Will be hidden
textLabel.hidden = true
// Will be visible
textLabel.hidden = false
Hope this helps! Good Luck!
Alexander Certosimo
1,774 PointsAlexander Certosimo
1,774 PointsHi Steven!
Thank you for the reply! Very interesting stuff i did not know about. So to follow up, essentially could i hide the Imageview, then have a button in my view that when pressed i can write some code under that action to show the image view and hide the text view? And vice versa?
Essentially what i would like to do specifically is have a button that says "Add image" when it was pressed, i would like the imageview to show and textview to hide and change the button text to show "Add text". Then when it was pressed again, switch it back to the way it was, textview visible and imageview hidden and button text showing "Add image". Is what i am brainstorming possible using what you mentioned above?
Thanks again for the help.
Steven Deutsch
21,046 PointsSteven Deutsch
21,046 PointsAlexander,
Yes this would be possible using this approach. You would want to set the hidden property of the items you don't want to see by default when you build the app in the attributes inspector. Then you would make it so when you press the button it sets their hidden property to false (making them appear). On this button press, you would also setTitle of the button to the new message you want to display. You can use conditional coding, either a ternary operator or if statements to check the state of the button's hidden property each time the button is pressed. If it is true, set to false - else set back to true. Does this make sense?
Alexander Certosimo
1,774 PointsAlexander Certosimo
1,774 PointsHi Steven
Totally makes sense. This is pretty much exactly what i was looking to do. So i could simply just write a function checking the hidden property? Basically button gets pressed, set title to "Add text", hide textview and show imageview. Then if the button is tapped again, do the same just in reverse. And i can do this with a simple if else? I am just trying to picture the cleanest way to implement it. I would not need a boolean check variable at all right? I can just check if the textView.hidden is = true or false
Steven Deutsch
21,046 PointsSteven Deutsch
21,046 PointsRight, the hidden property is set to a boolean value. So, theres only two options - true or false. Therefore, if you check for true and do some action, then you can have an else clause that handles the only other possibility - false.
Alexander Certosimo
1,774 PointsAlexander Certosimo
1,774 PointsGot it. Thanks so much for your help, Steven. Much appreciated.
Steven Deutsch
21,046 PointsSteven Deutsch
21,046 PointsHey Alexander Certosimo,
I have a slack channel for people learning iOS Swift development. We work on some group projects and walk through material together. If you would like to join the group, provide me with your email and I can send you an invite.
Alexander Certosimo
1,774 PointsAlexander Certosimo
1,774 PointsThank sounds great Steven. I have been self teaching myself for a while now and always run into small problems or questions that i need to look to someone with more knowledge for. Any way i can send you a PM here through the site? So i don't post my email in the forums?