how to Remove Dynamically Created UIView and UILabel text? - ios

I am adding some dynamic UIView named as *cellSeparator and other UILabels...now what happen is when i again call this code then its rewrite the label text and overwrite on previously created label text...i am not very much aware to this ios development.so can anyone please tell me how can i remove this UIView dynamically before creating again?beacause UIView is dynamically created i dont know how to remove that UIview
UILabel *indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-150, self.view.frame.size.width/2,30)];
[indexLabel setBackgroundColor:[UIColor clearColor]];
indexLabel.textColor = [UIColor whiteColor];
indexLabel.text = #"Details:-";
indexLabel.font = [UIFont systemFontOfSize:20.00];
UILabel *tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-120, self.view.frame.size.width/2, 30)];
tagLabel.backgroundColor = [UIColor clearColor];
NSLog(#"LOg %#",imageId);
NSLog(#"LOg %#",imageStyle);
NSLog(#"LOg %#",imageType);
NSLog(#"LOg %#",imageWeight);
tagLabel.text = [NSString stringWithFormat:#"The Id of Jewl Is: %#",imageId];
imageTypelabel= [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-90, self.view.frame.size.width/2, 30)];
imageTypelabel.backgroundColor = [UIColor clearColor];
imageTypelabel.text = [NSString stringWithFormat:#"The Type of Jewl Is: %#",imageType];
imageStylelabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-60, self.view.frame.size.width/2, 30)];
imageTypelabel.backgroundColor = [UIColor clearColor];
imageStylelabel.text = [NSString stringWithFormat:#"The style of Jewl Is: %#",imageStyle];
imageWeightlabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-30, self.view.frame.size.width/2, 30)];
imageStylelabel.backgroundColor = [UIColor clearColor];
imageWeightlabel.text = [NSString stringWithFormat:#"The weight of Jewl Is: %#",imageWeight];
imageWeightlabel.backgroundColor = [UIColor clearColor];
imageWeightlabel.textColor = [UIColor whiteColor];
imageTypelabel.textColor = [UIColor whiteColor];
imageWeightlabel.textColor = [UIColor whiteColor];
tagLabel.textColor = [UIColor whiteColor];
UIImage *imageBegin = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:imageBegin];
UIView *cellSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,545, self.view.frame.size.width ,3)];
cellSeparator.tag=1;
[cellSeparator setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth];
[cellSeparator setContentMode:UIViewContentModeTopLeft];
[cellSeparator setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:cellSeparator];

You could write a method to remove all the subviews of the view and modify this code according to your need.
- (void)removeSubviewsOfView
{
NSArray *subViews = [self.view subviews];
for(UIView *view in subViews)
{
[view removeFromSuperview];
}
}

Related

how to remove subview from class method in ios?

I have an app.In this app so many class methods used.I want to know is it possible to remove subview from class mehtod?
+(void)addPregressIndicator:(NSString *)strText view:(UIView *)ShowView
{
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView *busyView = [[UIView alloc] initWithFrame:appFrame];
busyView.backgroundColor = [UIColor clearColor];
busyView.opaque = YES;
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 95, 95)];
container.layer.borderWidth = 5;
container.layer.cornerRadius = 10;
container.layer.borderColor = [UIColor whiteColor].CGColor;
[container setBackgroundColor:[UIColor blackColor]];
UIActivityIndicatorView *av = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
av.center = CGPointMake(container.center.x, 34);
av.hidesWhenStopped = NO;
[av startAnimating];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 95, 30)];
lbl.text = strText;
lbl.textAlignment = NSTextAlignmentCenter;
lbl.center = CGPointMake(container.center.x, 70);
lbl.textColor = [UIColor whiteColor];
lbl.backgroundColor = [UIColor clearColor];
[container addSubview:av];
[container addSubview:lbl];
container.center = busyView.center;
[busyView addSubview:container];
[ShowView addSubview:busyView];
}
+(void)removePregressIndicator:(UIView *)hideView;
{
// i want remove busyview that subview in viewcontroller.
}
Please help me to remove subivew.
You can set a unique tag for the busyView like this busyView.tag = your unique tag;
And then you can remove the view with this code
+(void)removePregressIndicator:(UIView *)hideView;
{
UIView *busyView = [hideView viewWithTag:your unique tag];
if (busyView){
[busyView removeFromSuperview];
}
}
You should make sure that the busyView tag is unique for all the subviews of hideView.
Assigne tag to a each view and access the view with tag and put it
+(void)removePregressIndicator:(UIView *)hideView;
{
UIView *viewToRemove = [self.view viewWithTag:assigne_tag];
[viewToRemove removeFromSuperview];
}
+ (void)removePregressIndicator:(UIView *)hideView;
{
UIView *progView=[self.view viewWithTag:YourTag];
[progView removeFromSuperview];
}

How can we insert multiple titles on UInavigationBar in ios

Hi i am beginner in iOS and in my project i need to insert "titles" at "left side center" and "right side center" on UInavigationBar as like below image please help me how should i do below my requirement
See below code and try :
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = [[UIScreen mainScreen] bounds];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, 44)];
lbl1.backgroundColor = [UIColor redColor];
lbl1.textAlignment = NSTextAlignmentCenter;
lbl1.text = #"Dashboard";
[self.navigationController.navigationBar addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, 44)];
lbl2.backgroundColor = [UIColor redColor];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[self.navigationController.navigationBar addSubview:lbl2];
}
You can customize the titleView of navigation bar. Read the official documentation here.
As per your question, I tried the below working code:-
-(UIView *)getTitleView
{
CGRect frame = [[UIScreen mainScreen] bounds];
UIView *viewTitle = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, self.navigationController.navigationBar.frame.size.height)];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, self.navigationController.navigationBar.frame.size.height)];
lbl1.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
lbl1.textAlignment = NSTextAlignmentCenter;
[lbl1 setTextColor:[UIColor whiteColor]];
lbl1.text = #"Dashboard";
[viewTitle addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, viewTitle.frame.size.height)];
lbl2.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
[lbl2 setTextColor:[UIColor whiteColor]];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[viewTitle addSubview:lbl2];
return viewTitle;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.titleView = [self getTitleView];
}
I divide the code in separate method to follow modular approach. For any part of code, which is of separate task, write that piece of code in different method. This will lead to more readability.

Warning A long-running Parse operation is being executed on the main thread? [duplicate]

This question already has answers here:
A long-running Parse operation is being executed on the main thread
(4 answers)
Closed 7 years ago.
A long-running Parse operation is being executed on the main thread is happening on this part of code from below, i can't seem to figure out how to rid the error. Any help on how to get rid of warning.
PFFile *image = (PFFile *)[wallObject objectForKey:KEY_IMAGE];
UIImageView *userImage = [[UIImageView alloc] initWithImage:[UIImage imageWithData:image.getData]];
userImage.frame = CGRectMake(0, 65, wallImageView.frame.size.width, 225);
[wallImageView addSubview:userImage];
here is code below
-(void)loadWallViews
{
//Clean the scroll view
for (id viewToRemove in [self.wallScroll subviews]){
if ([viewToRemove isMemberOfClass:[UIView class]])
[viewToRemove removeFromSuperview];
}
//For every wall element, put a view in the scroll
int originY = 10;
for (PFObject *wallObject in self.imageFilesArray){
//Build the view with the image and the comments
UIView *wallImageView = [[UIView alloc] initWithFrame:CGRectMake(10, originY, self.view.frame.size.width - 20 , 330)]; //self.view.frame.size.height
//[[UIView appearance] setBackgroundColor:[UIColor redColor]]; //added for problem solve
//Add the image
PFFile *image = (PFFile *)[wallObject objectForKey:KEY_IMAGE];
UIImageView *userImage = [[UIImageView alloc] initWithImage:[UIImage imageWithData:image.getData]];
userImage.frame = CGRectMake(0, 65, wallImageView.frame.size.width, 225);
[wallImageView addSubview:userImage];
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, wallImageView.frame.size.width,55)];
infoLabel.text = [wallObject objectForKey:#"newsTitle"];
infoLabel.font = [UIFont fontWithName:KEY_FONT size:20];
infoLabel.textColor = [UIColor darkGrayColor];
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.numberOfLines = 0;
[wallImageView addSubview:infoLabel];
//Add the info label (User and creation date)
NSDate *creationDate = wallObject.createdAt;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:KEY_DATEFORMAT];
//Add the comment
UILabel *commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, wallImageView.frame.size.width, 12)];
//commentLabel.text = [NSString stringWithFormat:#"Uploaded by: %#, %#", [wallObject objectForKey:#"NewsDetail"], [df stringFromDate:creationDate]];
commentLabel.text = [NSString stringWithFormat:#": %#, %#", [wallObject objectForKey:#"newsDetail"], [df stringFromDate:creationDate]];
commentLabel.font = [UIFont fontWithName:KEY_FONT size:10];
commentLabel.textColor = [UIColor lightGrayColor];
commentLabel.backgroundColor = [UIColor clearColor];
[wallImageView addSubview:commentLabel];
UILabel *readLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 71 , 50, wallImageView.frame.size.width, 12)];
readLabel.text = #"Read more";
readLabel.font = [UIFont fontWithName:KEY_FONT size:10];
readLabel.textColor = [UIColor blueColor];
readLabel.backgroundColor = [UIColor clearColor];
[wallImageView addSubview:readLabel];
UIButton *faceBtn = [[UIButton alloc] initWithFrame:CGRectMake(2,310, 20, 20)];
[faceBtn setImage:[UIImage imageNamed:#"Facebook.png"] forState:UIControlStateNormal];
[faceBtn addTarget:self action:#selector(share:) forControlEvents:UIControlEventTouchUpInside];
[wallImageView addSubview:faceBtn];
UIButton *twitBtn = [[UIButton alloc] initWithFrame:CGRectMake(32,310, 20, 20)];
[twitBtn setImage:[UIImage imageNamed:#"Twitter.png"] forState:UIControlStateNormal];
[twitBtn addTarget:self action:#selector(share:) forControlEvents:UIControlEventTouchUpInside];
[wallImageView addSubview:twitBtn];
UIButton *tumblrBtn = [[UIButton alloc] initWithFrame:CGRectMake(62,310, 20, 20)];
[tumblrBtn setImage:[UIImage imageNamed:#"Tumblr.png"] forState:UIControlStateNormal];
[tumblrBtn addTarget:self action:#selector(share:) forControlEvents:UIControlEventTouchUpInside];
[wallImageView addSubview:tumblrBtn];
UIButton *yourBtn = [[UIButton alloc] initWithFrame:CGRectMake(92,310, 20, 20)];
[yourBtn setImage:[UIImage imageNamed:#"Flickr.png"] forState:UIControlStateNormal];
[yourBtn addTarget:self action:#selector(share:) forControlEvents:UIControlEventTouchUpInside];
[wallImageView addSubview:yourBtn];
// wallImageView = UIEdgeInsetsMake(0.0f, myCell.frame.size.width, 0.0f, 400.0f);
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 343, self.view.frame.size.width, .8)];
separatorLineView.backgroundColor = [UIColor grayColor];// you can also put image here
[wallImageView addSubview:separatorLineView];
[self.wallScroll addSubview:wallImageView];
originY = originY + wallImageView.frame.size.width + 1;
}
//Set the bounds of the scroll
self.wallScroll.contentSize = CGSizeMake(self.wallScroll.frame.size.width, originY);
//Remove the activity indicator
[self.activityIndicator stopAnimating];
[self.activityIndicator removeFromSuperview];
It's probably this:
[[UIImageView alloc] initWithImage:[UIImage imageWithData:image.getData]]
Looks like you're getting images synchronously instead of asynchronously.
Instead of using UIImageView, you should use PFImageView, which will download the file asynchronously if it's not already cached.

IOS, setting up Gesture Tap on UILabel thats inside a UIScrollView

I have a UIScrollView and inside this Scroll View i have some controls. Specifically a label thats underlined and i have setup Tap Gesture for the UILabel but doesn't seem to fire the methods that the Tap Gesture is bound to.
Here is the code:
-(void)setupUserNameControls:(UIScrollView*)scroll{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 23, 70, 16)];
label.textColor = [UIColor colorWithRed:59.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
label.text = #"First Name";
label.font = [UIFont fontWithName:#"Helvetica" size:14];
[scroll addSubview:label];
UIView *textView = [[UIView alloc] initWithFrame:CGRectMake(88, 16, 215, 28)];
textView.backgroundColor = [UIColor colorWithRed:60.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
[scroll addSubview:textView];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(15, 55, 70, 16)];
label2.textColor = [UIColor colorWithRed:59.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
label2.text = #"Last Name";
label2.font = [UIFont fontWithName:#"Helvetica" size:14];
[scroll addSubview:label2];
UIView *textView2 = [[UIView alloc] initWithFrame:CGRectMake(88, 48, 215, 28)];
textView2.backgroundColor = [UIColor colorWithRed:60.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
[scroll addSubview:textView2];
UIImageView *imageViewSep = [[UIImageView alloc] initWithFrame:CGRectMake(10, 92, 300, 2)];
UIImage *imageSep = [UIImage imageNamed:#"seperator.png"];
imageViewSep.image = imageSep;
[scroll addSubview:imageViewSep];
UIView *viewImage = [[UIView alloc] initWithFrame:CGRectMake(10, 108, 58, 58)];
viewImage.backgroundColor = [UIColor colorWithRed:132.0f/255 green:132.0f/255 blue:132.0f/255 alpha:1.0f];
[scroll addSubview:viewImage];
UIImageView *imageViewUser = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 48, 48)];
UIImage *imageUser = [UIImage imageNamed:#"defaultuser.jpg"];
imageViewUser.image = imageUser;
[viewImage addSubview:imageViewUser];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:#"Click to select your user image"];
[attributeString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,[attributeString length]}];
UILabel *labelPickImage = [[UILabel alloc] initWithFrame:CGRectMake(88, 126, 215, 16)];
labelPickImage.numberOfLines = 0;
labelPickImage.textColor = [UIColor colorWithRed:59.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
labelPickImage.attributedText = attributeString;
labelPickImage.font = [UIFont fontWithName:#"Helvetica" size:14];
[scroll addSubview:labelPickImage];
UITapGestureRecognizer *userImageTextTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(userImageSelectTap:)];
[userImageTextTap setNumberOfTouchesRequired:1];
[userImageTextTap setNumberOfTapsRequired:1];
[labelPickImage addGestureRecognizer:userImageTextTap];
}
-(void)userImageSelectTap:(UIGestureRecognizer *)sender{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}else{
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *) Picker {
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
This is how i set up the UIScrollView and pass it in:
UIScrollView *userInfoScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 108, screenRect.size.width, screenRect.size.height - 40 - viewTop.bounds.size.height - viewTopBorder.bounds.size.height)];
[userInfoScrollView setBackgroundColor:[UIColor colorWithRed:228.0f/255 green:228.0f/255 blue:228.0f/255 alpha:1.0f]];
userInfoScrollView.userInteractionEnabled = YES;
[self.viewUser addSubview:userInfoScrollView];
make sure the label stays in front of the scrollView.
[scroll bringSubviewToFront:labelPickImage];
Also,
labelPickImage.userInteractionEnabled = YES;

How to add multiple UILabels programmatically in Xcode?

Hello in Xcode I am trying to add two labels to my image programmatically but when I add the second one it will overwrite my first one. Here is my code:
//first label
UILabel* caption = [[UILabel alloc] initWithFrame:
CGRectMake(-25, 0, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentLeft;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:#" #%#",[data objectForKey:#"username"]];
[self addSubview: caption];
//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:
CGRectMake(-25, -200, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentCenter;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:#" Testing"];
[self addSubview: bottomBox];
I've tried changing the "0 to -200 to modify the Y coordinates so that the second label will shift down but it just overwrites the first one for some reason. Any help would be greatly appreciated thanks.
All the code for your 2nd label is referencing the 1st variable caption instead of bottomBox. You want:
//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 320, 50)];
bottomBox.backgroundColor = [UIColor whiteColor];
bottomBox.textColor = [UIColor blackColor];
bottomBox.textAlignment = UITextAlignmentCenter;
bottomBox.font = [UIFont systemFontOfSize:16];
bottomBox.text = #"Testing";
[self addSubview: bottomBox];

Resources