Nothing happens when addSubview image - objective-c

Here is the code:
CGRect myImageRect = CGRectMake(0, 0, 100, 100);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"Angry0.png"]];
[mainView addSubview:myImage];
Angry0.png is in my Resources folder. No image is display on simulator.

Right... You need to initalize all your views...
If I'm right your view hierarchy is this:
-mainView
-contentView
-scrollView
-imageView
So somewhere when you load mainView you also need to initalize all the child views...
For example:
...
//put this where you call mainView = [[UIView alloc] init...
contentView = [[UIView alloc] init...
//config the contentView
scrollView = [[UIScrollView alloc] init...
//config the scrollView
....
Now, providing that you correctly set your views frames and initialized them they shouldn't be null...
You can check by calling:
if (!scrollView) {
NSLog(#"scrollView is null :(");
}
Then you should be able to call your existing code and all should be good

Related

lag scrolling scrollview with tableview inside it

I have important problem with UIScrollView.
I want to make one app like yahoo weather and using this source code : BTGlassScrollView
in this source code exist one view with name "foregroundView" that is info view. this info view appear when scrolling my ScrollView from bottom to top.
now I make and add my ScrollView by this code:
_viewScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width + 2*blackSideBarWidth, self.view.frame.size.height)];
[_viewScroller setPagingEnabled:YES];
[_viewScroller setDelegate:self];
[_viewScroller setShowsHorizontalScrollIndicator:NO];
[self.view addSubview:_viewScroller];
_glassScrollView1 = [[BTGlassScrollView alloc] initWithFrame:self.view.frame BackgroundImage:[UIImage imageNamed:#"background3"] blurredImage:nil viewDistanceFromBottom:0 foregroundView:[self customView]];
[_viewScroller addSubview:_glassScrollView1];
in my code I write [self customView] that make me info view and this is customView function:
- (UIView *)customView {
//this method is for make UITableView
[self makeTableBuy:[[self.tableDic objectForKey:#"person"] count]];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tableBuy.frame.size.width, _tableBuy.frame.size.height)];
view.layer.cornerRadius = 3;
view.backgroundColor = [UIColor colorWithWhite:0 alpha:.3];
[view addSubview:_tableBuy];
return view;
}
- (UITableView*)makeTableBuy:(int)numberOfRow {
_tableBuy = [[UITableView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,numberOfRow*TABLE_HEIGHT) style:UITableViewStyleGrouped];
[_tableBuy setTag:1];
[_tableBuy setBackgroundColor:[UIColor clearColor]];
_tableBuy.showsVerticalScrollIndicator = NO;
_tableBuy.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableBuy.rowHeight = TABLE_HEIGHT;
_tableBuy.scrollEnabled = NO;
_tableBuy.delegate = self;
_tableBuy.dataSource = self;
return _tableBuy;
}
now When I'm scrolling my ScrollView my info view (my tableview) moving to top and appear but my scrolling have so lagging.
please guide me how to fix this lag???

Why is *drawRect* not being called from the ViewController?

I have an iPad app (XCode 5, iOS 7, Storyboards and ARC). I have a UIPopover created in a UIViewController, and when I tap a button it correctly displays the popover.
Now, I want to draw a grid in that popover; I know I have to add code to drawRect do do the drawing, and do a setNeedsDisplay to get the drawing done. However, it's not working!
My question is why is drawRect not being called when from [popoverView setNeedsDisplay]; ? Both methods are in the same UIViewController class.
Here is the code that is supposed to call the drawRect
// create popover
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 650, 416)];
popoverView.backgroundColor = [UIColor whiteColor];
popoverContent.preferredContentSize = CGSizeMake(650.0, 416.0);
// draw the lines, etc on the popoverContent
[popoverView setNeedsDisplay];
// create the popover controller and attach the popover content to it
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate = (id)self;
[popoverController setPopoverContentSize:CGSizeMake(650, 416) animated:NO];
[popoverController presentPopoverFromRect:CGRectMake(650, 416, 10, 50) inView: obViewOpenAppts
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
I have a breakpoint in drawRect so I know it's not being called.
What am I doing wrong?
drawRect: is a UIView method, not a UIViewController method. If you want to override it you have to subclass UIView, and then create it like this:
MyUIViewSubclass *popoverView = [[MyUIViewSubclass alloc] initWithFrame:CGRectMake(0, 0, 650, 416)];

how to make a imageview do a segue

I have a bunch of imageview generated in a gridview. But now I want that each imageview can do a segue to another screen. Does anybody knows how I can do that? Here is the code of how I generated my imageview and put it into my grid.
UIImageView * myView = [[UIImageView alloc] initWithImage:myImage];
CGRect rect = myView.frame;
rect.origin.x = xCurrent;
rect.origin.y = yCurrent;
myView.frame = rect;
myView.tag = cellCounter;
[gridContainerView addSubview:myView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
myView.userInteractionEnabled = YES;
[myView addGestureRecognizer:tap];
Please help
Kind regards
You are looking for a tapGestureRecognizer. Add one of those to your image views and make the action call your segue method.
UIImageView *img = [[UIImageView alloc] init];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(mySequeMethod:)];
img.userInteractionEnabled = YES;
[img addGestureRecognizer:tap];
A:
use a custom button instead of UIImageView, and set the image... and then segue with that button, (it looks the same)
B:
use a gesture recognizer to the view, and use a tap with one finger.

On view hierarchy, clarification needed

In this code example, i am trying to produce the following view hierarchy
Window -> background image -> scroll view -> text view
All i see however is
Window -> background image
What am i missing please?
-(void) viewWillAppear:(BOOL)animated {
UIScrollView *scrollWindow = [[UIScrollView alloc]
initWithFrame:CGRectMake(30, 30, 440, 212)];
UITextView *scrollableText = [[UITextView alloc] init];
[scrollableText setEditable:NO];
[scrollableText setText:#"Why, hello there"];
[scrollWindow addSubview:scrollableText];
UIImage *backgroundImage = [[UIImage alloc] initWithCGImage:
[UIImage imageNamed:#"about_bg.png"].CGImage];
UIImageView *backgroundView = [[UIImageView alloc]
initWithImage:backgroundImage];
[backgroundView addSubview:scrollWindow];
[[self view] addSubview:backgroundView];
}
Andrew is right about not making the scroll view a subview of the background UIImageView view. But the scroll view is invisible. Only its content (scrollableText) will show. And you haven't set scrollableText's frame, so it's effectively invisible too. Init like so:
[scrollableText setEditable:NO];
[scrollableText setText:#"Why, hello there"];
[scrollableText setFrame:CGRectMake(0, 0, 100, 100)];
And you should see it.
What you need is this hierarchy:
Window
background image view
scroll view
text view
Try adding two subviews to your window, not shove them all as subviews into each other.''-
- (void) viewWillAppear:(BOOL)animated {
UIScrollView *scrollWindow = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 30, 440, 212)];
UITextView *scrollableText = [[UITextView alloc] init];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"about_bg.png"]];
scrollableText.editable = NO;
scrollableText.text = #"Why, hello there";
[scrollWindow addSubview:scrollableText];
[self.view addSubview:backgroundView];
[self.view addSubview:scrollWindow];
}

Cocoa Touch - Adding a UIImageView programmatically?

How can I create and position a new imageview in objective-c?
Thanks!
I tried this but it doesnt seem to do anything...
-(void)drawStars{ //uses random numbers to display a star on screen
//create random position
int xCoordinate = arc4random() % 10;
int yCoordinate = arc4random() % 10;
UIImageView *starImgView = [[UIImageView alloc] initWithFrame:CGRectMake(xCoordinate, yCoordinate, 58, 40)]; //create ImageView
starImgView.image = [UIImage imageNamed:#"star.png"];
[starImgView release];
I placed this method in my viewcontroller. I see some CG stuff do I need to import core graphics or something? (what is core graphics anyway?)
You are asking about iPhone image view. Right? Then try this
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
This will create an image view of 100 x 50 dimension and locating (10, 10) of parent view.
Check the reference manual for UIImageView and UIView. You can set an image by using image property of imageView.
imgView.image = [UIImage imageNamed:#"a_image.png"];
And you can use the contentMode property of UIView to configure how to fit the image in the view. For example you can use
imgView.contentMode = UIViewContentModeCenter to place the desired image to the center of the view. The available contentModes are listed here
You haven't added your view as a subview to another view, meaning it isn't in the view hierarchy.
Assuming you are doing this in a view controller, it might look something like:
[self.view addSubview: imgView];
(void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.frame.size.width, self.view.layer.frame.size.height)];
imgView.image = [UIImage imageNamed:#"emptyCart.jpeg"];
imgView.backgroundColor = [UIColor whiteColor];
imgView.contentMode = UIViewContentModeCenter;
[self.view addSubview: imgView];
}
I had problem with passing the value NSSArray to NSString,
I got the answer:
//Display all images....
NSString *imgstring= [self.allimages objectAtIndex:indexPath.row];
cell.myimages.image=[UIImage imageNamed:imgstring]; //displaying Images in UIImageView..noproblem.
// Display all numbers...
cell.mylables.text=[NSString stringWithFormat:#"%#", [mysetobjectAtIndex:indexPath.row ]]; //showing results fine ArghyA..