UIImageView not showing up - objective-c

I have a UIImageView with property, it's connected in the storyboard and in viewDidLoad I have set it's image. I first create an image then assign it to the image of the UIImageView.
-(void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [[UIImage imageNamed:#"image"]resizeableImageWithCapInsets:UIEdgeInsetsMake(28.5, 5, 6.5, 5)];
[self.imageView setImage:image];
}
I've made sure numerous times that the UIImageView is connected in the IB and it's showing up as connected. I have a UICollectionView over it, but it doesn't extend high enough to cover it all and the UICollectionView background color is set to clear. Thanks in advance for the help.

I suppose you added the IBOutlet UIImageView * imageView in your .h and then linked it in the Interface Builder, BUT did you add the property of the imageView? Like this:
#property (nonatomic, retain) IBOutlet UIImageView * imageView;
And then in your .m file, under the #implementation you need to add #synthesize imageView;
and set the image to your imageView using only: [imageView setImage:image]; (without the self.).
(You can also skip the #synthesize and just use [_imageView setImage:image];)
The #property allows you to edit the properties of the object, like backgroundColor, alpha, image, etc.

Related

UIButton label doesn't appear on UIImageView

I user Interface Builder to position a UIButton and a UIImageView superimposed.
In the code, I change button label if image exists
In Example.h
#property (strong, nonatomic) IBOutlet UIButton *takePicture;
#property (strong, nonatomic) IBOutlet UIImageView *image;
In Example.m
[self.image setImage:aPhoto];
(...)
NSString *pictureButtonTitle = myCondition#"Changer la photo":#"Ajouter une photo";
(...)
[self.takePicture setTitle:pictureButtonTitle forState:UIControlStateNormal];
I use this code for a view, and I see "Change picture" correctly on my picture when myCondition is true.
But in another view, nothing appears !! WHY ?
You could either do it in the interface builder - as you did, or do it in your viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view bringSubviewToFront:self.button];
}
Arranging the outlets in the IB as you did mean that the lower object will be on top of the others... The button will be "front"/on top when it is placed below all other objects as you shown in the screenshot.
If you wish to arrange the outlets differently, you could use these methods as well:
[self.view insertSubview:self.button aboveSubview:self.imageView];
Or:
[self.view insertSubview:self.imageView belowSubview:self.button];
Well, I found out : in Interface Builder, it seems that the order you place your objects matters
If you place an UIImageView before UIButton, it works well.
I see the label
But if you do the reverse : button is under the image.
And the label is not displayed
I still don't know if it's the only way to rearrange these items ?

How can I rotate a UIImageView?

i'm pretty new to Xcode, and i was wondering how to rotate a uiimage on touch down, this is what i have currently:
#implementation secondViewController
int degrees;
UIImageView *gliderImageView;
bool continueSpinning;
-(void)startSpinning {
degrees = 0;
continueSpinning = true;
[self continueSpinning];
}
-(void)continueSpinning {
degrees = (degrees + 1) %360;
CGAffineTransform rotate = CGAffineTransformMakeRotation( degrees / 180.0 * 3.14);
[gliderImageView setTransform:rotate];
if(!continueSpinning) return;
else [self performSelector:#selector(continueSpinning) withObject:nil afterDelay:0.1f];
}
-(void)stopSpinning {
continueSpinning = false;
}
so does anyone know
how to link the button to the image
how to set a boolean with a button
if this code will work in the first place?
You can find an example of rotating a UIImage here.
So just in your .h write this:
(in the #interface)
IBOutlet UIImage *myImage;
IBOutlet UIButton *myButton;
(after the #interface closing bracket)
#property (nonatomic, retain) IBOutlet UIImage *myImage;
#property (nonatomic, retain) IBOutlet UIButton *myButton
-(IBAction) spinIt:(id)sender;
Then in your .m write:
Under the #implementation:
#synthesize myImage, myButton;
Then below the end of the viewDidLoad write something like this:
-(IBAction)spinIt:(id)sender {
myImage.image = image;
[self addSubview:imageView];
CGAffineTransform rotate = CGAffineTransformMakeRotation( 1.0 / 20.0 * M_PI );
[imageView setTransform:rotate];
}
The above code is from here.
After that add an image and a button to your view in the Storyboard and set up the references by control dragging from the button to the responder and clicking the spinIt method and by control dragging from the outlet names on the far right in the connections tab under the little arrow icon to their respective connections.
The above code rotates 90 degrees so you may want to change it to fit your needs. Also, this is a duplicate question, so in the future please search before posting. Good luck!
EDIT- If you want the image to spin while the button is being pressed I recommend making an array of images of it at different intervals in the spin and playing the images like a stop motion animation. Instructions on that can be found [here]3.

Can't change the frames of .xib UI objects in code

I am trying to re-write my code to allow me to use xibs to design custom UIViews for re-use throughout my projects. I have created a class called SlidesView which has a .xib of the same name. I have set it up using the following IBOutlets:
#property (nonatomic, retain) IBOutlet UIImageView *ivMain;
#property (nonatomic, retain) IBOutlet UILabel *lHeader;
#property (nonatomic, retain) IBOutlet UITextView *tvMain;
#property (nonatomic, retain) IBOutlet UIImageView *ivIcon;
I have made sure these are all properly connected within the .xib file. I also have a function which I want to use to setup all the initial data for the view:
- (void)setupUsingCenter:(CGPoint)center mainImage:(UIImage *)mainImage mainText:(NSString *)mainText header:(NSString *)header andIconImage:(UIImage *)iconImage
The 'setupUsingCenter:self ...' body looks as follows:
// First check for a main image and image icon
if (mainImage != nil)
{
// Set the image
[self.ivMain setImage:mainImage];
[self.ivMain setFrame:CGRectMake(10, 10, 10, 10)];
// If image icon set that too
if (iconImage != nil)
{
[self.ivIcon setImage:iconImage];
}
}
And finally I load this .xib using the following code:
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"SlidesView" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[SlidesView class]])
{
SlidesView *slidesView = (SlidesView *)view;
[slidesView setupUsingCenter:self.vPlaceholder.center mainImage:slideImage mainText:slideText header:slideTitle andIconImage:slideIcon];
[slidesView setTextColours:[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f]];
[self.view addSubview:slidesView];
self.viewPopup = slidesView;
}
}
I have put in the random setFrame parameters for testing purposes but for some reason the frame doesn't change. However, the image on the ivMain does change. Can anyone explain how I can change the frame for my UI objects? Also, why does it allow me to change the image but the frame seems un-editable, doesn't this mean it must be initialised?
Any help is much appreciated,
Thanks.
Are you sure autolayout is disabled on the .xib ? It is my understanding that you cannot edit frames when autolayout is on.
If that's the case, you can either disable autolayout for that particular .xib, or you can work on constraints instead of the frame property
The use of autosizing may cause some unexpected behaviors also the autoresize subviews.
You can try something like this:
[View setAutoresizingMask:UIViewAutoresizingNone];
And:
[View setAutoresizesSubviews:NO];
Where View is the UIView that you want to apply that behavior.

Unable to implement simple view scrolling in iOS

I'm having a hard time implementing a simple scroll on my detail view.
The app is straightforward with a Master and Detail views.
When the user taps an item on Master, the Detail view is pushed with larger photo, blog text and etc.
I would like the entire Detail view to scroll, so if the picture is tall, or the text is long, they can scroll vertically to see/read more. I do not want the user to scroll these items individually. It should feel like webpage scrolling.
Currently my Detail view loads OK but I can't make it scroll.
My DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController {
IBOutlet UILabel *postTextLabel; // wired to Text Label
IBOutlet UILabel *postAuthorNameLabel; // wired to Author Label
}
#property (strong, nonatomic) id detailItem;
#end
My DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}
- (void)configureView
{
if (self.detailItem) {
NSDictionary *post = self.detailItem;
NSString *postText = [post objectForKey:#"post_text"];
NSString *postAuthorName = [post objectForKey:#"post_author_name"];
postTextLabel.text = postText;
postAuthorNameLabel.text = postAuthorName;
}
}
#end
Structure on IB:
Any ideas on what's missing to make this work?
I would do the following:
1)(optional)turn your View into a scrollview by dragging it into the view in the scruture list on the side.
2)link the scrollView into your viewcontroller .h and make an Outlet connection something like this
#property (strong, nonatomic) IBOutlet UIScrollView *scroller;
(make sure you add #synthesize in the .m if you add this manually)
and make sure it is connected in IB!
3)set the contentsize of the scrollview in viewDidLoad method
scroller.contentSize = CGSizeMake(320,550);
NOTE: IBOutlet UILabel *postTextLabel; should actually probably be a UITextView so you can access ContentSize
Which would allow for the following.
CGRect frame = postTextLabel.frame;
frame.size = postTextLabel.contentSize;
postTextLabel.frame = frame;
scroller.contentSize = CGSizeMake(320, frame.size.height+200);//200 or how ever much space is above the textView
and again that only works if you use a UITextView
for ipad replace 320 with 768 or 1024, or whichever depending on orientation
The best way to connect it in IB is like this holding down control and dragging to the .h file
and make sure it is set to automatic like in the picture, and pointing to> the .h of your view.
Adding it like this also automatically adds the #synthesize for you.
Make sure UserInteractionsEnabled are checked here for our scrollview

(iOS) How can I Reuse an UIImageView in another View?

I have a view controller with a UIScrollView with a heap of UIImageViews as subviews. There is a segue to another view controller but i want to reuse the UIImages that are already in memory rather than load them again for efficiency's sake. But if I point my new UIImageView to an existing one, nothing shows up.
I had to use an intermediate pointer variable (imageViewPointer) as if I assign it directly in the prepareForSegue method, the NSLog's from each view controller would show different addresses. This workaround NSLogs show the same address but still nothing shows up.
If I use reassign the .image property instead, the image shows up but the images from the first view controller are load asynchronously so the .image property might change.
In the first view controller
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NextViewController *nextViewController = [segue destinationViewController];
nextViewController.imageViewPointer = [[myScrollView subviews] objectAtIndex:myIndex];
NSLog(#"%#", [[myScrollView subviews] objectAtIndex:myIndex]);
}
In the second view controller
#interface nextViewController : UIViewController
#property (retain, nonatomic) IBOutlet UIImageView *imageView;
#property (retain, nonatomic) UIImageView *imageViewPointer;
#implementation nextViewController
#synthesize imageView, imageViewPointer;
{
self.imageView = self.imageViewPointer;
// [self.imageView setNeedsDisplay]; Doesn't help
NSLog(#"IMAGE VIEW POINTER = %#", self.imageViewPointer);
NSLog(#"IMAGE VIEW = %#", self.imageView);
[super viewDidLoad];
}
If you simply assign a view like that, it won't appear in the view hierarchy of your current view controller's view. You'll need to do something like:
[self.view addSubview: imageView];
However, I wouldn't recommend that. It is better to just assign the image of the view to the image of the other view.
self.imageView.image = self.imageViewPointer.image;
Either you reuse the UIImage or the method you are doing is a bit wrong as you are assigning the imageViewPointer to imageView which is(imageViewPointer) not a subview of your view controller's view. So add subview imageViewPointer or imageView after assigning imageViewPointer.