UIImageView automatically removing itself from self.view - ios

tutorialImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Tap to Start.png"]];
tutorialImage.frame = CGRectMake(0, 0, 1024, 768);
[tutorialImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(blankMethod)]];
tutorialImage.userInteractionEnabled = YES; // i use this line and the previous line so that the user can't press any buttons behind the image
tutorialImage.alpha = 0;
[self.view addSubview:tutorialImage];
[self.view bringSubviewToFront:tutorialImage];
[UIView animateWithDuration:1.0f animations:^{
tutorialImage.alpha = 1;
} completion:^(BOOL finished) {
[self.view addSubview:tutorialImage]; // this line makes the image come back
}];
I know you probably won't be able to deduce the problem just from this code, but is there anything in that code that makes the tutorialImage auto remove itself from it's superview?
Anyway, during the UIView animation the image fades in for a bit like normal, then it disappears. If I add that last line of code there (the commented one), the UIView animation will make the image fade in and flash once halfway through. I just added this image and there is no code telling it to remove itself from superview.
Let me know if you have any ideas as to fixing the problem or showing you more code, I'll check frequently.
Also, I've tried restarting the simulator which didn't work, and the tutorial image is declared in the h file UIImageView *tutorialImage;. The console doesn't show any errors or anything when the problem occurs or anything.
Edit:
Ok, strange. I altered the declaration in the H file from UIImageView *tutorialImage; to #property (strong, nonatomic) UIImageView *tutorialImage; then used _tutorialImage fixed the problem. Is this something to do with the strong parameter? I'll mark who ever can explain what was going on as correct.

When you have a weak reference, ARC will dealloc the object once there are no more retains on it (when no object is pointing at the object with a strong pointer). When you changed the #property to strong, you are now telling ARC to keep the object around until the parent (your view controller) is dealloc'ed.

Related

programmatically added uiview does not displayed

I'm trying to create a uiview programmatically and adding it to a stack_view which was also programmatically created and added to the view.
This is the code:
the .h
#interface ViewController : UIViewController
#property (strong, nonatomic, nullable) UIStackView * stack;
#end
the .m
#implementation viewController
#synthesize stack;
- (void) viewDidLoad {
[super viewDidLoad];
CGRect * vframe = self.view.frame;
// - Option 1
stack = [[UIStackView alloc] initWithFrame:CGRectMake(vframe.origin.x, 100,vframe.size.width,300)];
stack.axis = UILayoutConstraintAxisHorizontal;
stack.aligment = UIStackViewAligmentTop;
stack.distribution = UIStackViewDistributionFill;
[self.vew addSubview:stack];
// Option 2
// stack.traslateAutorezisingMaskIntoConstraints = NO;
// [stack.leadingAnchor constraintsEqualToAnchor: self.view.leadingAnchor].active = YES;
// [stack.topAnchor constraintsEqualToAnchor: self.view.topAnchor constant: 100].active = YES;
UIView * pView = [[UIView alloc] init];
pView.backgroundColor = [UIColor blueColor];
[stack addArrangedSubview:pView];
}
#end
This code does not show the view at all, I've tried to prove the option 2 (appeared commented in the code) and it does not work either. It is not supposed that the view, upon inserted in the stack, will get the size of the stack, since the distribution of it is "Fill"?. None of this work either even if I define Pview with frame=CGRectMakeRect(0,0,self.view.frame.size.width,100), for instance.
What am I doing wrong?
EDIT: I already fix the misspelled in the code (the * in the CGRect and the self.vew instead of self.view). I made these mistakes when I was manually copying the code, I did not copy and paste the code; tha's is why is made them and that's why the original code compile well
To diagnose this first test the pView. Init it with a fixed size like CGRectMake(100,100,100,100) and add it directly to the viewContoller's view. You should be able to see it no problem if not you have a deeper issue.
If that goes well try the UIStackView with a fixed size. Color its background to see it better if you need. If you still dont see it then double check it still has the correct frame in viewDidAppear of the viewController. It might have adjusted itself after creation. If that is correct go to Debug -> View Debugging -> Capture View Heirachy in Xcode after you have it up in the simulator. If you dont see it there then there was an issue adding it as a subview (note the typo [self.vew addSubview:stack];)
If that goes well then there is a problem with [stack addArrangedSubview:pView]. Similar to the previous step, loop through all the arrangedSubviews in UIStackView in viewDidAppear of its viewController.
CGRect vframe = self.view.frame;
In viewDidLoad, your self.view.frame has not been calculated yet. You need to do this in viewDidLayoutSubviews or viewDidAppear. both of these will get called multiple times so be careful.
EDIT:
As suggested below by danh (I overlooked it) you should remove * from the above line , also, you have several misspelling in your code, don't know how this code really even compiled for you.

Correct way to animate position and size of a UIView with image (this method breaks in Xcode 6)?

I have an app where I am simultaneously animating the position and size of a UIView with an image by calling setFrame inside a animateWithDuration, thusly:
UIImage *image = [UIImage imageNamed: imageName];
[_faceView setImage: image];
[_faceView setFrame: CGRectMake(-40.0, 0.0, 40.0, 40.0)];
[UIView animateWithDuration:0.5 animations:^{
[_faceView setFrame: CGRectMake(80.0, 140.0, 160, 160)];
}
completion:^(BOOL finished) {}
When compiled in Xcode 5, and running in either iOS 7 or iOS 8, I have obtained the correct behaviour using the above methodology.
Now, compiling in Xcode 6 (and hence against SDK 8.0, which I assume is the defining difference?), and running in either iOS 7 or iOS 8, this code does not achieve the correct result: what appears to happen is that the image is animated to its "natural" size rather than to that specified in the CGRect.
So: (a) does anyone know why this might be? and (b) either way, is the basic problem that I'm doing something wrong? -- is there a more "correct" way to achieve an animation of position/size of an image in a UIView that will achieve the desired result?
By default in Xcode6 even if you don't have any constraints associated with a view - IB will create default ones. That's why after setting the frame view is getting repositioned to the original frame.
You can add line
_faceView.translatesAutoresizingMaskIntoConstraints = YES;
And this essentially will break existing constraints, but you will get proper animation.
However I would suggest to re-do this animation to use auto-layout constraints and not use strict frame setting.
I think you can try to begin to use Autolayout to make animation. As I remembered, it has been recommended in the WWDC session.
As a simplest example, if you want to move a view right 40 point in animation , you can add a constraint to the left, use Interface Builder to link it to a IBOutlet like
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftConstraint;
And the animation code like follow:
- (IBAction)animatinoButtonPressed:(id)sender {
self.leftConstraint.constant = self. leftConstraint.constant + 40.f;
[UIView animateWithDuration:3.f animations:^{
[self.view layoutIfNeeded];
}];
}
The method layoutIfNeeded will force the view to rearrange the subviews immediately, which move the target view and make animation.
I think you can create suitable constraints in your case and make correct animation as well.

Add overlay image on top of drawRect

I have a UIView subclass and I am drawing in the drawRect method.
How can I add an overlay image on top of the drawRect target layer?
I add the following in the initWithFrame method:
//add the image overlay
UIImageView *overlageImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"downloaderFront.png"]];
overlageImage.center = self.center;
[self.layer addSublayer:overlageImage.layer];
//replacing the above line with below line does not fix either
[self addSubview:overlageImage];
This image is not visible on top of the drawRect drawing.
The stuff with the layer is certainly wrong. Just do this:
UIImageView *overlageImage =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"downloaderFront.png"]];
[self addSubview:overlageImage];
You can play with the frame of overlageImage to get the position where you want it.
As to your actual question: put a breakpoint. I suspect you'll find that your code is never even running (because your view's initWithFrame is never called). That's just a guess - you didn't say anything about how this UIView subclass instance gets into the interface - but it's a common enough mistake, and one should always suspect the obvious first!

Upraded Xcode, I have 2 Identicle views with UIScrollView. One View adds components to UIScroll fine, the other does not

I have an app that has been working just fine for ages. I upgraded XCode and now 1 of my views is doing weird things.
None of the objects I attempt to add to the UIScrollview show up.
I have 2 screens that are basically identical in code (i literally copy and pasted). One of them works just fine, I click a button to move to the other screen, and it doesn't display ANY components I attempt to add to the scroll view.
Any ideas as to what could be wrong? Why it would suddenly stop adding components after upgrading Xcode?
I have a background image that I have tested and changed to self.view that changes just fine. But anything I try to add to my UIScrollView, nothing gets added. Some sample code...
my .h declaration
#interface OutgateInputViewController : UIViewController<UITextFieldDelegate,UIPickerViewDelegate>{
IBOutlet UIScrollView *outputsView;
a sample in my .m
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"inputbackground.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
int x = 20;
int y = 20;
CGRect myImageRect = CGRectMake(0, 15, 320, 32.5);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
NSString *imgName = #"screen_logo.png";
[myImage setImage:[UIImage imageNamed:imgName]];
[outputsView addSubview:myImage];
y = y + 30;
pretty standard stuff. its an EXACT copy up to this point of the screen that works. (even a copy long after, i just pull data from a different location). So any ideas on what changed?
I went back and re-did everything from scratch, and apparently for some reason THIS View needs the additional __weak at the start of the declaration in the .h ...
no idea why as of yet, but this solved the issue of the entire view being broken.
__weak IBOutlet UIScrollView *outputsView;

Shadow in separate view from UIImage for dynamic adjustments

I would like to obtain this effect (http://stackoverflow.com/questions/7023271/how-to-adjust-drop-shadow-dynamically-during-an-uiimageview-rotation) but from a more complex image than just a red square ! If the link ever gets broken, it's a about how to adjust drop shadow dynamically during an UIImageView rotation.
So I tried implementing something but I just can't get the shadow in a separate layer... Here is my code, very simple, but doesn't work:
// here is my code
- (void)viewDidLoad {
[super viewDidLoad];
testView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"handNoShadow.png"]];
testViewShadow = [[UIView alloc] initWithFrame:testView.frame];
testViewShadow.layer.shadowPath = [[testView layer] shadowPath];
testViewShadow.layer.shadowOpacity = 1.0;
testViewShadow.layer.shadowOffset = CGSizeMake(10, 10);
testViewShadow.layer.shadowRadius = 5.0;
[self.view addSubview:testViewShadow];
[self.view addSubview:testView];
}
PS: i did #import
I do get an image but no shadow... =(
Any lead, help, code, link... is welcome !
Thanks
possible cause:
your testViewShadow.clipToBounds property is set to YES (should be NO)
your testViewShadow do the drawing of the shadow correctly but another UIView is on top and mask it. Check your Z order. Either the order in Storyboard/Nib file (or the order you added the subviews programmatically). Last in the list (or last one added) is on top. For my app I had to put the UIView that need a shadow last so that no other view mask it.

Resources