Xcode - UILabel is not changing value on viewDidAppear - xcode

Hey everyone, I have a iPhone App I am creating. It uses a uitableview and that goes to a detail view.
Now on the detail view I have a scroll view, and all the data (XML) comes into it fine, each time, no issues.
My UILabels in my scrollview do update with the correct data, but, they keep adding subviews on top of each other to keep adding label after label and it all looks mumbo jumbo.
I have tried everything to try and remove the subviews such as:
for(UIView *subview in [scrollView subviews]) {
[subview removeFromSuperview];
}
AND
[[scrollView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
So I am stumped, I got no idea what is happening. Any help? I have been searching Google for ages and agesm, but keep finding the same answers that don't work :(
Any direction to sites that might help that you know of will be greatly appreciated.
Thanks!
Here is what I have in my controller that loads all the scrollview and labels:
- (void)viewDidAppear:(BOOL)animated {
// Create the scroll view for this view
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(320, 270);
[scrollView setFrame:CGRectMake(0, 198, 320, 170)];
// Do the labels for the text
UILabel *dates = [[UILabel alloc] initWithFrame:scrollView.bounds];
dates.textColor = [UIColor whiteColor];
dates.font = [UIFont boldSystemFontOfSize:12];
dates.text = round_date;
dates.tag = 1;
[dates setBackgroundColor:[UIColor clearColor]];
[dates setFrame:CGRectMake(10, 10, 280, 22)];
[scrollView addSubview:dates];
[dates release];
// Add the content to the main scrollview
[self.view addSubview:scrollView];
[scrollView release];
}

Ok, the reason for this is that viewDidAppear will get called every time you present the view.
If you do this is
-(void) viewDidLoad;
instead.
To make sure you can access them later you might want to keep a reference to them in your UIViewController
-(void) viewDidLoad {
scrollView = [[UIScrollView alloc] initWithFrame];
[self.view addSubview:scrollView];
}
and define it in your header file with:
UIScrollView *scrollView;

Related

Activity Indicator will not go away

I am using the following code to show an Activity Indicator while some data is loading. The problem is that when I try to hide it again. The Activity Indicator stays put. The screen lightens a bit but that is it.
To show it:
self.overlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activityIndicator.center = self.overlayView.center;
[self.overlayView addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];
[self.tableView addSubview:self.overlayView];
To hide it:
[self.activityIndicator stopAnimating];
[self.overlayView removeFromSuperview];
Try to add your overlayView to the superview of your tableView :
[self.view.superview addSubview:self.overlayView];
I figured it out with a combination of stackoverflow questions. Here is my code to show the UIActivityIndicatorView
-(void)loadView {
[super loadView];
self.overlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activityIndicator.center = self.overlayView.center;
[self.overlayView addSubview:self.activityIndicator];
[self.tableView addSubview:self.overlayView];
[self.activityIndicator startAnimating];
}
Here is the code to hide it. It looks like putting the code in the loadView event made all the difference.
[overlayView removeFromSuperview];

last row of uicollectionview is not visible while scrolling

I created UiCollectionView programmatically. This is my code snippet...
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
collectionV=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, 500) collectionViewLayout:layout];
[collectionV setDataSource:self];
[collectionV setDelegate:self];
[collectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[collectionV setBackgroundColor:[UIColor greenColor]];
collectionV.scrollEnabled = YES;
[self.view addSubView:CollectionV];
My problem is I could not see the last row while scrolling. Anybody help me to fix it.
Thanks in advance.

How do you create a UIImageView programmatically in Xcode

I just want to make an UIImageView programmatically that displays a 20by20 dot at point (50,50).(The image of the dot is called draw.png). For some reason nothing shows up on the screen.
Heres my code:
- (void)viewDidLoad
{
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
dot.image=[UIImage imageNamed:#"draw.png"];
[self.view addSubview:dot];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
}
First, make sure draw.png exists in your project and that you are referencing it exactly (so if it's draw.PNG you should put #"draw.PNG"). Also, call [super viewDidLoad] before everything.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
dot.image=[UIImage imageNamed:#"draw.png"];
[self.view addSubview:dot];
}
Try this :-
UIImageView *imageview = [[UIImageView alloc]
initWithFrame:CGRectMake(50, 50, 20, 20)];
[imageview setImage:[UIImage imageNamed:#"AppleUSA1.jpg"]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:imageview];
In Swift :
let imageView = UIImageView(frame: CGRect(x: 50, y: 50, width: 20, height: 20))
imageView.image = UIImage(named: "draw.png")
self.view.addSubview(imageView)
This code is correct. Make sure draw.png exists in your project. You can do it by checking if [UIImage imageNamed:#"draw.png"] doesn't return nil.
Also, make sure you don't have another view on top of your image view.
make sure that image is in your project..
UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
[imgView setImage:[UIImage imageNamed:#"xyz.jpg"]];//if your images extension is .png than no need to write extension of an image..
[self.view addSubview:imgView];
This code is perfect match an UIImageView:
UIImageView *IMG_view= [[UIImageView alloc] initWithFrame:CGRectMake(20 ,50 ,40 ,40)];
[IMG_view setTag:100]; //[IMG_view setTag:indexPath.row];
IMG_view.layer.borderWidth= 0.5 ;
IMG_view.layer.borderColor= [[UIColor clearColor] CGColor];
IMG_view.layer.cornerRadius= 3;
IMG_view.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:IMG_view];
IMG_view.image=[UIImage imageNamed:#"Loading_50x50.png"];

Add a label to UINavigationBar in addition to the titleView

I'm using a UINavigationController to push my views. I want to add a UILabel above the title in the UINavigationBar of the root view controller.
When I use the following code, the label appears, but under the UINavigationBar:
[self.view addSubview:newLabel];
Is there a way to add the label to the UINavigationBar or to the UIView so that it appears above the navigation bar?
I know I can create a new UINavigationBar and build it with multiple controls like this:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navBar.barStyle = UIBarStyleBlack;
navBar.translucent = YES;
[self.view addSubview:navBar];
[navBar release];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.text = #"My Text";
[navBar addSubview:navLabel];
[navLabel release];
However, doesn't that defeat the purpose of using the navigation controller?
You can refer this site, may be it help you in sorting your problem
http://iphonesdevsdk.blogspot.com/2011/04/uinavigationbar.html

UIScrollView viewForZoomingInScrollView when pagingEnabled with multiple UIImageViews in it

I have this UIScrollView with pagingEnabled to let the user page between some images.
Now I want to let the user zoom on each one of the images. How do I do that? What I have now zooms on my UIScrollView from its origin and I need it to zoom on each picture, not the hole UIScrollView.
What I have now:
UIScrollView* containerView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
containerView.scrollEnabled = YES;
containerView.pagingEnabled = YES;
[containerView setMaximumZoomScale:2.0];
[containerView setDelegate:self];
self.view = containerView;
UIImage *imageOne = [UIImage imageNamed:image01];
UIImageView *viewOne = [[UIImageView alloc] initWithImage:imageOne];
viewOne.frame = CGRectMake(0, 44, 320, 480);
[containerView addSubview:viewOne];
UIImage *imageTwo = [UIImage imageNamed:image02];
UIImageView *viewTwo = [[UIImageView alloc] initWithImage:imageTwo];
viewTwo.frame = CGRectMake(320, 44, 320, 480);
[containerView addSubview:viewTwo];
UIImage *imageThree = [UIImage imageNamed:image03];
UIImageView *viewThree = [[UIImageView alloc] initWithImage:imageThree];
viewThree.frame = CGRectMake(640, 44, 320, 480);
[containerView addSubview:viewThree];
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)containerView
{
return containerView;
}
Thanks in advance
You have to return a view contained in your scrool. As you have 3 UIImage, try with this...
In .h
UIView *auxView;
In .m
...
[auxView addSubview:imageOne];
[auxView addSubview:imageTwo];
[auxView addSubview:imageThree];
[containerView addSubview:auxView];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)containerView{
return auxView; }
I hope this help you
I think you mean you want to zoom each UIImageView individually, right?
I was struggling with this problem many weeks, then I decide to wrote a class to solve this problem.
Here is my github repo of this class: https://github.com/windmemory/PhotoCollectionView
My solution to this problem is to create single UIScrollView to contain every UIImageView, and then add these UIScrollView to the containerView.
If we add all the UIImageView to a single view then make it zoomable, when we zoom one photo, all these photos will be zoomed, then the contentSize will change accordingly. Once the contentSize is changed, it will messed up all the paging thing. So the best way I came up is to use UIScrollView to contain UIImageView, then put every UIScrollView into the containerView, this will allow you to zoom each photo separately, which won't mess up all the paging thing you set up.
Hope this may help you.

Resources