Xcode - Image view not hiding - ios

I have an image view in a basic app that I am attempting to set to hidden on load using:
- (void)viewDidLoad
{
my_image.hidden = YES;
}
This code along with some other attribute changes are not functioning at all. I have synthesized the my_image property. Any ideas why this may not be working? Also, please let me know if you need any further information. I'm new to this and it's really bugging me, so thanks in advance!

It's hard to make it clear with a simple code line, my_image.hidden = YES;
But I think you can do the things below :
Print the imageView (your 'my_image' object) in the console to look into it.
Use the other properties of the imageView to see if you can manipulate it, such as changing the frame, setting the background color, or setting an image for it. If you can change the frame, then you may set my_image.hidden = NO; somewhere else.
Create another UIImageView object and add try it!
If none of above works, you can set the frame of 'my_image' to CGRectZero to hide it.

Maybe you forgot to connect the IBOutlet ? Are you using .xib ?

make a breakpoint at line of my_image.hidden = YES; Does it go into the breakpoint ? If it goes into , make sure the my_image is not nil.

UIImageView *imageview = [[UIImageView alloc]init];
[imageview setHidden:YES];

Setter worked for me:
UIImageView * bb = (UIImageView*)[self.view viewWithTag:1];
[bb setHidden:YES];

I had the same problem with hiding the image view but if you remove the #property statement the hidden behaves as it it should.
Set the hidden in viewdidLoad then the IBaction - works fine
.h
IBOutlet UIImageView *crackedimage1;
.m
-(void)viewdidLoad
crackedimage1.hidden = YES;
- (IBAction)crackaction1:(id)sender {
crackedimage1.hidden = NO;};

Related

UIImageView won't recognize tap gesture

I've searched through other questions similar to this on SO already, and came up empty with a solution to my problem. For some reason, my UIImageView (which I've added to a UIView in storyboard) just won't recognize a tap gesture I've created. User Interaction is enabled, as is multiple touch. My selector method just never gets called.
self.image is my imageView property that I've added onto a UIView.
- (void)viewDidLoad {
[super viewDidLoad];
self.image.image = [UIImage imageNamed:#"mountainWallpaper.png"];
[self gesture];
}
-(void)gesture {
UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(imageTapped:)];
//imageTap.numberOfTapsRequired = 1;
//imageTap.numberOfTouchesRequired = 1;
imageTap.delegate = self;
[self.image addGestureRecognizer:imageTap];
}
-(IBAction3)imageTapped:(UIGestureRecognizer *)tap {
NSLog(#"test");
}
Any suggestions? I've looked through most other questions I found and tried out various solutions with no such luck. Many answers said to verify that user interaction is enabled, to make sure the delegate is set...I've tried much of what has been suggested already.
I've also experimented with first adding the UIView as the tap gesture target, adding the imageView itself as the target, adding the gesture to both the UIView and the imageView...nothing.
In storyboard, I have my UIImageView connected to my viewController as both the view and the image. I generally do things programmatically so I have to get used to connecting objects in IB, I'm pretty bad at it.
Check for following points may be it will helpful for you :
Make sure your UIImageView User interaction is enable .
Image View priority should be on top over super view .
make sure super view of Image view userIntration is also enable .
have you added UI gesture Delegate ?
It looks like you have a spelling mistake. Try changing IBAction3 to IBAction or void:
-(void)imageTapped:(UIGestureRecognizer *)tap {
NSLog(#"test");
}

Changing tab bar item background color without affecting the tint

I am new to iOS and I was stuck in design. I have searched through several posts but none of them helps.
Most of the posts have suggested to replace the whole tab bar item with image which I don't think is a good idea.
I would expect something like configuration in the storyboard (e.g: User Defined Run Time Attributes) that can help to change the background color of an item without affecting its TINT COLOR.
This is the result I am trying to achieve.
You can set background image tabbar like this way, it would be better to customize the tabbar controller by this way.
In Header file
#import <UIKit/UIKit.h>
#interface CustomTabBarController : UITabBarController
-(void) setBGView;
#end
In M file.
#import "CustomTabBarController.h"
#implementation CustomTabBarController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
-(void)viewDidLoad
{
[super viewDidLoad];
[self setBGView];
}
-(void)setBGView
{
// Background
UIImageView* bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TabBG.png"]] ; //You can create single color image or Single Color view to add
bgView.frame = CGRectMake(0, self.view.frame.size.height-60, elf.view.frame.size.widdth, 60);
[self.view addSubview:bgView];
#end
HTH, Enjoy Coding!!
Thanks to #ViralSavaj and tagyro from
here
I manage to achieve the output similar to my question, seem like "setSelectionIndicatorImage" is the only way to achieve the output like this.
However, the output might not be good when you run in other devices, one way to resolve this problem is to draw a shape according to the device resolution, you can get the snippets from the above link
beware of the code you reused from #tagyro
CGSize tabSize = CGSizeMake(CGRectGetWidth(self.view.frame)/5, 49);
you might want to change the /5 according to your number of tabs, in my case, i only need to put 4 to get the job done

UIImage view does not display the image

I'm having a few issues with displaying a UIImage in an app that I'm developing. The steps I took to do this are as follows:
1 - Add a UIImageView to my main view controller in the storyboard. I did this using the interface builder.
2 - I then created an IBOutlet for the UIImageView in the viewcontroller.h. I ensured proper referencing of the image view from the outlet.
3 - I set the image for the UIImageView like so:
_fireIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"flame_icon.png"]];
This was done within the viewDidLoad method in the viewcontroller.m file.
Troubleshooting:
I checked whether the _fireIcon variable is being set. It is.
I checked whether the viewDidLoad method is being called. It is.
In addition, the image is displayed when I statically set it via the storyboard itself. But when I do it via code, it does not display.
Let me know if you need any more additional information, or code snippets.
you need not to initialize an imageview as you are using Interface builder for image view
_fireIcon.image = [UIImage imageNamed:#"flame_icon.png"]; // or [_fireIcon setImage:[UIImage imageNamed:#"flame_icon.png"]];
This sets the image.
When we drop the UI elements using Interface builder , we need not initialize them, as they are taken care by apple.
Doing this you reset your property with newly created UIImageView:
_fireIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"flame_icon.png"]];
And your reference to UIImageView from storyboard gets compeletely lost.
Instead you want to set image:
_fireIcon.image = [UIImage imageNamed:#"flame_icon.png"];
Firstly, you do need to allocate the Element created via Interface builder, you just need to set the image to UIImageView directly like this :-
[myImage setImage:[UIImage imageNamed:#"5.jpg"]];
Make sure image name is correct and its extension too. I just tried and its working great here. Let me know if you still face problem here.
Swift 3.0
statusImageView?.image = UIImage(named: "select.png")

iOS navigationItem.titleView remove margins

I have something like this:
But I need that the view that am setting for the titleView, doesn't has those margins on the left and right side. Something like this:
This is what am actually doing:
#property (nonatomic, strong) XHScrollMenu *scrollMenu; // where XHScrollMenu is a subclass of UIView
- (void)viewdidLoad{
_scrollMenu = [[XHScrollMenu alloc] initWithFrame:self.navigationController.navigationBar.frame];
_scrollMenu.backgroundColor = [UIColor clearColor];
_scrollMenu.delegate = self;
_scrollMenu.selectedIndex = 0;
self.navigationItem.titleView =self.scrollMenu;
}
I tried giving the view a with of 320, but I got the same result. I read in other post, that maybe a subclass make the trick, but don't know how to implement that solution...
How can I make the title view use the entire width?
Try with bounds instead of frame. see different between bounds and frame.
...initWithFrame:self.navigationController.navigationBar.bounds] /// or give frame manually such like CGRectMake(x,y,self.view.frame.size.width, yourHeight);
If this suggestion doesn't help you then sure problem under initWithFrame of XHScrollMenu. Go in this method and check there.

iOS: Can't show view after hiding it

I have a activityIndicator and a transparent black layer behind it. When something loads I show them like this:
[activityIndicator startAnimating];
loadingCover.hidden = NO;
And when I hiden them, I just do it like this:
[activityIndicator stopAnimating]; //hides on stop
loadingCover.hidden = YES;
So far everything works. But somehow if I want to show them again, it doesn't work. Any ideas?
EDIT:
This is how I do it...
- (void)viewDidLoad {
[mainView addSubview:loadingCover]; //works
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[loadingCover removeFromSuperview]; //works
}
- (void)refreshRSS:(id)sender {
[mainView addSubview:loadingCover]; //doesn't work
}
EDIT2:
First of all I'm coding with ARC mode and second, loadingCover has been changed to loadingplate, no biggie...
Ok so in my .h file I do this:
UIView *loadingplate;
UIActivityIndicatorView *loadingIndicator;
And in my .m file in viewDidLoad I do this:
CGRect viewRect = CGRectMake(0, 0, 320, 460);
loadingplate = [[UIView alloc] initWithFrame:viewRect];
UIColor *loadingplateColor = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
[loadingplate setBackgroundColor:loadingplateColor];
loadingplate.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin;
loadingIndicator = [[UIActivityIndicatorView alloc] initWithFrame:viewRect];
[loadingIndicator setContentMode:UIViewContentModeCenter];
[loadingIndicator startAnimating];
loadingIndicator.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin;
[loadingplate addSubview:loadingIndicator];
[mainView addSubview:loadingplate];
EDI3:
After reading Jasarien's answer I tried this in my .h file:
#property (strong, nonatomic) UIView *loadingPlate;
And then in my .m file:
#synthesize loadingPlate = _loadingPlate;
In this file in viewDidLoad I'm creating the view.
But this doesn't seem to work either.
Please see XJones' comment. This answer is probably wrong.
After Edited question:
It seems that since you're using arc, your loadingPlate view is probably being released too soon.
When you alloc and init your loadingPlate view, it'll have a retain count of +1, as expected from an alloc/init. Yet, because you're using ARC, after you add it as a subview of your main view, ARC releases it. Effectively the loadingPlate view is owned by its super view and when you remove it from the superview, the superview releases it, causing the view to be deallocated. Thus when you try to show it again, it's not there anymore and can't be shown.
The solution to this would be to create a property for your loadingPlate view, and give it a 'strong' reference, (effectively the same as declaring the property as a 'retain'). This way, the object that has this property will own the loadingPlate view, keeping a strong reference to it.
You're saying loadingCover.hidden = NO does not work? Have you added the view as a subview to the current showing view?
Try moving the allocation and initialization of that view to the init method. Also, when you call the method to add it again, one thing you can try is to simply check if it exists first probably by:
if (!loadingPlate) {
// allocate and initialize again because somehow it has been released.
}
At least this way you know if the instance of loadingPlate is still being retained.
Your basic logic to create, show, and hide loadingPlate looks fine. I use a very similar implementation abstracted in a UIView subclass in my apps. All of the answers are stabs in the dark b/c the code you posted doesn't show an obvious problem other than the discrepancies in your iVar names. I decided to add this answer in the hope that it helps you find the solution.
You don't need a property. As far as retain semantics go, iVars are strong by default. If you do create a property and synthesize it the way you show than be sure to refer to either self.loadingPlate or _loadingPlate in your code.
Make sure you don't have a typo in your iVar name vs your property name. For example, if you have an iVar called loadingplate but your property is loadingPlate then you will end up with two iVars (loadingplate and loadingPlate) as properties automatically create iVars if they don't match one already defined.
Your logic of creating the loadingPlate view in viewDidLoad and adding/removing it from the superview as needed is totally fine. Just make aure you don't have loadingPlate = nil anywhere other than in viewDidUnload.
If it's possible for the superview frame size to change you should explicitly set loadingPlate.frame = mainView.bounds before adding loadingView as a subview. If the frames don't change this won't matter.
If I think of anything else I'll add it later. Good luck.
Are you sure loadingCover doesn't still have its hidden property set to YES from the previous time displayed?
Try:
- (void)refreshRSS:(id)sender {
loadingCover.hidden = NO;
[mainView addSubview:loadingCover]; //doesn't work
}
Don't you have to use self.loadingplate to call the property strong and make the retain on your view ?

Resources