How to remove all subviews?
Edit: With thanks to cocoafan: This situation is muddled up by the fact that NSView
and UIView
handle things differently. For NSView
(desktop Mac development only), you can simply use the following:
[someNSView setSubviews:[NSArray array]];
For UIView
(iOS development only), you can safely use makeObjectsPerformSelector:
because the subviews
property will return a copy of the array of subviews:
[[someUIView subviews]
makeObjectsPerformSelector:@selector(removeFromSuperview)];
Thank you to Tommy for pointing out that makeObjectsPerformSelector:
appears to modify the subviews
array while it is being enumerated (which it does for NSView
, but not for UIView
).
Please see this SO question for more details.
Note: Using either of these two methods will remove every view that your main view contains and release them, if they are not retained elsewhere. From Apple's documentation on removeFromSuperview:
If the receiver’s superview is not nil, this method releases the receiver. If you plan to reuse the view, be sure to retain it before calling this method and be sure to release it as appropriate when you are done with it or after adding it to another view hierarchy.
How to remove all subviews of a certain type
Iterate over your maps subviews and remove all UIImageViews
:
func removeDots() {
for case let dot as UIImageView in yourPainMapView.subViews {
dot.removeFromSuperView()
}
}
In case you are using other UIImageView
subViews you do not want to remove, subclass UIImageView
(class MyDot:UIImage {...}
):
for case let dot as MyDot in yourPainMapView.subViews
How to remove all subviews of a view in Swift?
EDIT: (thanks Jeremiah / Rollo)
By far the best way to do this in Swift for iOS is:
view.subviews.forEach({ $0.removeFromSuperview() }) // this gets things done
view.subviews.map({ $0.removeFromSuperview() }) // this returns modified array
^^ These features are fun!
let funTimes = ["Awesome","Crazy","WTF"]
extension String {
func readIt() {
print(self)
}
}
funTimes.forEach({ $0.readIt() })
//// END EDIT
Just do this:
for view in self.view.subviews {
view.removeFromSuperview()
}
Or if you are looking for a specific class
for view:CustomViewClass! in self.view.subviews {
if view.isKindOfClass(CustomViewClass) {
view.doClassThing()
}
}
What is the best way to remove all subviews from you self.view?
[self.view.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
It's identical to your variant, but slightly shorter.
Swift: Remove All from the Subview
wand1 = UIImageView(... is rewriting your reference over and over, so you will never be able to remove anything but the last item created from the superview. You are either going to have to use an array or dictionary:
class Class
{
var array = [UIImageView]();
...
func something()
{
...
for(var i = 0; i < 6; i++){
let wand1 = UIImageView();
wand1.image = wand
array.append(UIImageView(frame: CGRectMake(wandX, wandY, feldBreite, feldBreite)))
self.add.Subview(wand1)//Dunno how this works but it is your code
wandXarray.insert(wandX, atIndex: i)
wandYarray.insert(wandY, atIndex: i)
wandX = wandX + feldBreite
}
...
func removeThisImage(index : Int)
{
array[index].removeFromSuperView();
}
Or you can create object references for every image you create, each with a unique name
//NO LONGER ALLOWED
If you just want to remove all subviews from a view and not concerned about removing specifics, just call self.subviews.removeAll()
where self is the view that contains your subviews.
//
Looks like you will have to write your own extension method to handle this:
extension UIView
{
func clearSubviews()
{
for subview in self.subviews as! [UIView] {
subview.removeFromSuperview();
}
}
}
Then to use it, it is just self.view.clearSubviews();
Remove all subviews except named
I wouldn't quite do it the way Sony suggested for a couple reasons.
By doing it that way, you'll be removing subviews while using that same subview array for enumeration.
You may also be removing the layout-related subviews, i.e.
UILayoutGuide
, that you didn't explicitly add to your view.
Instead, I recommend iterating over a separate array containing a copy of your original self.view.subviews
array and excluding any subviews of type UILayoutSupport
:
var subviews = self.view.subviews
for subview in subviews as [UIView] {
if subview != toolBarOne && subview != toolBarTwo && !(subview is UILayoutSupport) {
subview.removeFromSuperview()
}
}
How to remove a subview (or all subviews of a view)
Since your UIView is autoreleased, you just need to remove it from the superview. For which there is the removeFromSuperview
method.
So, you'd just want to call [self.tabsView removeFromSuperview]
. As long as your property declaration is set to retain
that's all you'll need.
Swift remove subviews from superview
By removing all subviews, you can also be removing subviews other than the ones you explicitly added, like the refresh view and layout constraints.
(And to answer your question from the comments, layout constraints can in fact be subviews. I explain how to remove all subviews except layout constraints here: https://stackoverflow.com/a/27281242/2274694.)
In general, I recommend changing your code to be more specific such that only the views you've added are removed. For example, you could add a tag to the subviews you add in your addPosts
method:
post.tag = 1000
then remove only the subviews with that tag in refresh:
:
let subViews = self.scrollView.subviews
for subview in subViews{
if subview.tag == 1000 {
subview.removeFromSuperview()
}
}
to ensure that you don't remove any subviews you haven't explicitly added yourself.
Edit: It turns out the OP's added subviews are all of custom type PostLineItem
so the tags are unnecessary since we can just remove all the PostLineItem
s instead:
for subview in self.view.subviews {
if subview is PostLineItem {
subview.removeFromSuperview()
}
}
Related Topics
How to Keep Uitableview Contentoffset After Calling -Reloaddata
In iOS 12, When Does the Uicollectionview Layout Cells, Use Autolayout in Nib
Force Landscape Mode in One Viewcontroller Using Swift
Create a Rectangle with Just Two Rounded Corners in Swift
How to Add a 'Done' Button to Numpad Keyboard in iOS
How to Simultaneously Satisfy Constraints - No Constraints in Place
How to I Import 3Rd Party Frameworks into Xcode Playground
Differencebetween Pan and Swipe in iOS
Swift - How to Detect Orientation Changes
Dismissmodalviewcontrolleranimated Deprecated
iOS 7 Status Bar Collides with Navigationbar
Storing Images Locally on an iOS Device