I have this code:
for(Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
return;
}
}
in this code I add an imageView on self.view but in my app i call this "for" many times and finally I have my self.view with 4 or 5 imageView "fix".
What's the way to remove all these imageView from my self.view?
If you only want to remove instances of UIImageView, you can try something like this:
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
[v removeFromSuperview];
}
}
Update:
As vikingosegundo wrote in the comments, you can do this instad.
If you add each imageview to an array, you can remove them from the view later on like this:
NSMutableArray *images = [[NSMutableArray alloc] init];
for Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
[images addObject:fix]; // Add the image to the array.
return;
}
}
The later on, remove them from the view:
for (UIImageView *v in images) {
[v removeFromSuperview];
}
Just call removeFromSuperview for every subView. Something like:
for(UIView *subview in self.view.subviews)
[subview removeFromSuperview];
NSMutableArray *images = [NSMutableArray array];
for Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
[images addObject:fix];
}
}
for (UIView *v in images){
[v removeFromSuperview];
}
Another approach
for(UIView *v in self.view.subviews)
if([v isKindOfClass:[UIImageView class]])
[v removeFromSuperview];
I put an example together.
Related
To show a help view I've put an UIImageView which behind has some UIButtons. How can I disable user interaction of these buttons? If I touch this image where buttons are behind, they responds to touch events.
CODE FOR IMAGE BACKGROUND:
self.helpBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
self.helpBackground.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
self.helpBackground.hidden = YES;
[self.view addSubview:self.helpBackground];
I've used self.helpBackground.userInteractionEnabled = NO; but didn't work.
Thanks.
Put your buttons in a array and loop through them and disable them:
NSArray *buttonsArray = #[yourButton1, yourButton2];
for (UIButton *button in buttonsArray) {
button.enabled = NO;
}
And when you want them enabled just loop again and set enabled to YES
Keep a Tag to your helpView(imageview) and add the following code
for (UIView *view in self.view.subviews) {
if (view.tag != yourViewTag) {
view.userInteractionEnabled = NO;
}
}
And after removing the help screen use the following code
for (UIView *view in self.view.subviews) {
view.userInteractionEnabled = YES;
}
you can try below solution..When help imageview is appearing
for (UIView *view in [self.view subviews])
{
if (view isKindOfClass:[UIButton Class])
{
view.userInteractionEnabled = NO;
}
else
{
view.userInteractionEnabled = YES;
}
}
////After dismissing the help screen you can
for (UIView *view in [self.view subviews])
{
if (view isKindOfClass:[UIButton Class])
{
view.userInteractionEnabled = YES;
}
}
////(OR) Simply do as below
self.view.userInteractionEnabled=YES;
Hope it helps you..
You can create a method that disables user interaction for all views that are under your helpBackground:
- (void)disableUserInteractionForViewsUnderView:(UIView *)view
{
CGRect area = view.frame;
for (UIView *underView in [self.view subviews]) {
if(CGRectContainsRect(area, underView.frame))
underView.userInteractionEnabled = NO;
}
}
and after that you call it where you need it:
[self disableUserInteractionForViewsUnderView:self.helpBackground];
EDIT
I've created a UIViewController category gist on github.com. You can find it here: https://gist.github.com/alexcristea/0244b50e503e8bf4f25d
You can use it like this:
[self enumerateViewsPlacedUnderView:self.helpBackground usingBlock:^(UIView *view) {
if ([view isKindOfClass:[UIButton class]]) {
view.userInteractionEnabled = NO;
}
}];
I'm trying to make a UISearchBar rectangular instead of rounded, but all the solutions I found so far (mostly iterating through subviews) seem broken on iOS 7.
I did some research myself and as it turns out, it only has a UIView subview, which has additional subviews, a UISearchBarBackground and a UISearchBarTextField (both of them are private classes).
I tried
if ([view isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[view removeFromSuperview];
}
and
if ([view conformsToProtocol:#protocol(UITextInputTraits)]) {
#try {
[(UITextField *)view setBorderStyle:UITextBorderStyleRoundedRect];
}
#catch (NSException * e) {
// ignore exception
}
}
where view is the subview of that one UIView subview but none of them seems to work.
Try this... (I know it is also using subview but it is working in ios7)
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, 320, 49)];
[self.view addSubview:searchBar];
[self checkSubViewsOfView:searchBar WithTabIndex:#""];
and Add this method
-(void)checkSubViewsOfView:(UIView *)view WithTabIndex:(NSString *)strTab
{
if ([view isKindOfClass:NSClassFromString(#"UISearchBarTextField")])
{
view.layer.borderWidth = 1;
view.layer.borderColor = [[UIColor whiteColor] CGColor];
return;
}
for (UIView *vvv in view.subviews)
{
NSLog(#"%#%#",strTab,[vvv description]);
if (vvv.subviews > 0)
{
NSString *str = [NSString stringWithFormat:#"____%#",strTab];
[self checkSubViewsOfView:vvv WithTabIndex:str];
}
}
}
you can set the searchfield-background like this:
[self.searchBar setSearchFieldBackgroundImage:[[UIImage imageNamed:#"searchbar_stretch-0-10-0-10"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)] forState:UIControlStateNormal];
and the searchbar-background like this:
[self.searchBar setBackgroundImage:[UIImage imageNamed:#"categories_navbar"]];
I`m having problem with removing view from superview.
Adding view:
- (void)createCircles
{
NSString *currentDate = [self currentDate];
NSArray *array = [self.horizontalScroll subviews];
UILabel *label = nil;
for (label in array)
{
if ([label.text isEqualToString:currentDate])
{
UIView *view = [[UIView alloc] initWithFrame:label.frame];
view.backgroundColor = [UIColor redColor];
[self.horizontalScroll insertSubview:view atIndex:0];
[self.labelsArray insertObject:view atIndex:0];
}
}
}
Trying to remove:
- (void)labelTouch:(UITapGestureRecognizer*)sender
{
NSArray *array = [self.horizontalScroll subviews];
UILabel *label = (UILabel*)sender.view;
for (int i = 0; i < [array count]; ++i)
{
UILabel *l = array[i];
if (label.tag == l.tag)
{
UIView *view = nil;
view = [self.labelsArray objectAtIndex:0];
view.hidden = YES;
[view removeFromSuperview];
view = nil;
[self.labelsArray removeObjectAtIndex:0];
}
}
}
But after touch view is still displaying. Tried to remove label (l) - it is removed
Try this,
[[[self.horizontalScroll subviews] objectAtIndex:0] removeFromSuperView];
You should store reference to this "unkillable" view in ivar or property. Initialize it in first method and call removeFromSupperView in second.
I am customizing my search bar with this but it is not showing any image. It remove its default image as well.
UITextField *searchField = nil;
for (UIView *subview in [[workLocationSearchBar.subviews lastObject] subviews]) {
NSLog(#"hey");
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
NSLog(#"inside");
break;
}
}
if (searchField) {
UIView *searchIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"circle.png"]];
if ([searchIcon isKindOfClass:[UIImageView class]]) {
}
NSLog(#"aye");
searchField.rightView = searchIcon;
searchField.leftView=searchIcon;
}
Try This:
[workLocationSearchBar setImage:[UIImage imageNamed:#"IMAGE_NAME"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:#"Sample.png"];
[self.view addSubview:imageView];
NSArray *subviews = [self.view subviews];
for(id element in subviews) {
if ([[element class] isKindOfClass:[UIImageView class]]) //check if the object is a UIImageView
{
NSLog(#"element is a UIImageView\n");
[element setCenter:CGPointMake(500., 500.)];
} else {
NSLog(#"element is NOT a UIImageView\n");
}
}
}
I expected the output to be "element is a UIImageView, but it's actually element is NOT a UIImageView. Why? It's not that there are other subviews. There is only one. Furthermore, when run, the image is displayed at 100,100, not 500,500 as expected.
Your check is wrong. You should call isKindOfClass: on object and not on class of object.
[element isKindOfClass:[UIImageView class]]
Try the Following code.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:#"Sample.png"];
[self.view addSubview:imageView];
NSArray *subviews = [self.view subviews];
for(UIView *view in subviews) {
if ([view isKindOfClass:[UIImageView class]]) //check if the object is a UIImageView
{
NSLog(#"element is a UIImageView\n");
[element setCenter:CGPointMake(500., 500.)];
} else {
NSLog(#"element is NOT a UIImageView\n");
}
}
}
You can also Check sub views by tag value:
initially set imageView.tag=101; //anything you want
for(UIView *subview in [view subviews]) {
if(subview.tag== 101)/*your subview tag value here*/
{
NSLog(#"element is a UIImageView\n");
} else {
NSLog(#"element is NOT a UIImageView\n");
}
}