Why doesn't this UIImageView Show up? - objective-c

If I set this UIImageView as an IBOutlet it works fine and shows up in the sim. But programmatically it isn't working. I think I am missing a step or forgetting a line of code.
It is declared as
#property (nonatomic,retain) UIImageView *circleView;
and the implementation
self.circleView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.circleView.center = self.view.center;
[self.view addSubview:circleView];

You just forgot to initialize your UIImageView that's why it's not working ...
self.circleView = [[UIImageView alloc] init];
self.circleView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.circleView.center = self.view.center;
[self.view addSubview:circleView];

Related

objective-c adding UILabel to center

hi i'm begginer in objective-c and i want to learn how i can make UILabel in center of screen
this is my code:
XXRootViewController.h
#interface XXRootViewController : UIViewController{
UILabel *title; } #end
XXRootViewController.m
#import "XXRootViewController.h"
#implementation XXRootViewController {
NSMutableArray *_objects;
}
- (void)loadView {
[super loadView];
self.view = [[[UIView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]]
autorelease];
self.view.backgroundColor = [UIColor whiteColor];
title = [[UILabel alloc] initWithFrame:CGRectMake(300,200,400,200)];
title.text = #"This is Sasuke's first app :)";
[self.view addSubview:title];
}
#end
Try this code i make some changes in you code
self.view = [[UIView alloc]
initWithFrame:[UIScreen mainScreen].bounds];
self.view.backgroundColor = [UIColor whiteColor];
title = [[UILabel alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width - 50,200)];
title.text = #"This is Sasuke's first app :)";
title.textAlignment = NSTextAlignmentCenter;
title.center = self.view.center;
[self.view addSubview:title];
You just need to set frame in center of the screen ...
change your one line code with it -
title = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 200,self.view.frame.size.height/2 - 100,400,200)];
now your label is shown in the center of screen .. This will help you change the frame according to your use...
Has Hardik Thakkar said, the best way would be to use the center method. That way, even if your label would grow, it will always be centred.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(300,200,400,200)];
label.text = #"something";
label.center = self.view.center; (this should always be you superview)
Now, even if your label is bigger than the size you were expecting, it will always be centred

Custom inputView of UITextField doesn't work

I want to set a custom view to the inputView property of a UITextField. (Basically because I want to add a UILabel to the selectionIndicator of a UIPickerView and I couldn't find an easier way...) If I set a UIPickerView as the inputView, everything works perfectly normal. But if I set a UIView as the inputView, it just doesn't show up when the UITextField becomes the firstResponder. Has anybody got an explanation for me here?
This works:
UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];
// configuration of field
UIPickerView* pickerView = [[UIPickerView alloc] initWithFrame:field.inputView.frame];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
field.inputView = pickerView;
This doesn't:
UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];
// configuration of field
UIView* view = [[UIView alloc] initWithFrame:field.inputView.frame];
view.backgroundColor = [UIColor redColor];
field.inputView = view;

How should I go about recreating the iOS home screen open folder animation?

At first I thought it wouldn't be too hard, but turns out there's a bit more to it than I thought.
At the moment my code's based around this: https://github.com/jwilling/JWFolders. However, it's not very clean, the biggest problem being that it takes a screenshot every time the folder opens (renderInContext: is pretty taxing on the CPU so turns out being very low fps). Aside from that this implementation doesn't look as clean and as polished as the Apple animation does.
Apple manages to pull off a pixel perfect animation that doesn't lag. I'd like to do the same! What is the best way to go about this/how would you do this - a cleanly coded, good looking and fluid animation? At the moment, my animation's lacking in all those areas, and I'm stumped about how I could do it better (like Apple does it).
-
I'm not begging for code, just a best method/how to. But if you want to go ahead and code it then there's no way I'd say no (that would definitely be bouty worthy)!!
I would separate the Springboard into several views, where each view is one row of icons.
If a folder icon is clicked, I would animate all following views to move down and up to make place for the folder content. at the same time I would add a subview that represents the folder content and animates with the same speed its size and position if necessary.
To support a background image that spans over the whole screen I would cut that into pieces and add this pieces as background image views to each row's view.
I wrote a quick and dirty sample code with the animation starting on any tap location, also available as complete project on github.
#interface ViewController ()
#property (nonatomic, strong) UIView *upperView;
#property (nonatomic, strong) UIView *lowerView;
#property (nonatomic, strong) UIImageView *upperImageView;
#property (nonatomic, strong) UIImageView *lowerImageView;
#property (nonatomic, strong) UIView *folderContentsView;
#property (nonatomic, assign) CGRect startRect;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *bgImg = [UIImage imageNamed:#"bg.png"];
self.upperImageView = [[UIImageView alloc] initWithImage:bgImg];
self.lowerImageView = [[UIImageView alloc] initWithImage:bgImg];
[self.upperImageView setContentMode:UIViewContentModeTop];
[self.lowerImageView setContentMode:UIViewContentModeTop];
self.upperView = [[UIView alloc] initWithFrame:self.upperImageView.frame];
self.lowerView = [[UIView alloc] initWithFrame:self.lowerImageView.frame];
[self.upperView addSubview:_upperImageView];
[self.lowerView addSubview:_lowerImageView];
[self.view addSubview:_lowerView];
[self.view addSubview:_upperView];
UITapGestureRecognizer *upperTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(openOverlay:)];
UITapGestureRecognizer *lowerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(openOverlay:)];
[self.upperView setUserInteractionEnabled:YES];
[self.upperView addGestureRecognizer:upperTapRecognizer];
[self.upperView setClipsToBounds:YES];
[self.lowerView setUserInteractionEnabled:YES];
[self.lowerView addGestureRecognizer:lowerTapRecognizer];
[self.lowerView setClipsToBounds:YES];
self.folderContentsView = [[UIView alloc] initWithFrame:CGRectZero];
self.folderContentsView.backgroundColor = [UIColor redColor];
UITapGestureRecognizer *closeTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(closeOverlay:)];
[self.folderContentsView addGestureRecognizer:closeTapRecognizer];
[self.view addSubview:self.folderContentsView];
[self.folderContentsView addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bgFolder.png"]]];
[self.folderContentsView setClipsToBounds:YES];
self.startRect = [self.upperView frame];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)openOverlay:(UITapGestureRecognizer *) sender
{
[self.upperView setUserInteractionEnabled:NO];
[self.lowerView setUserInteractionEnabled:NO];
CGPoint location = [sender locationInView:sender.view];
self.folderContentsView.frame = CGRectMake(0, location.y,
_lowerView.frame.size.width, 0);
self.lowerView.frame = CGRectMake(0, location.y,
_lowerView.frame.size.width, _lowerView.frame.size.height);
self.upperView.frame = CGRectMake(0, 0,
_upperView.frame.size.width, location.y);
self.lowerImageView.frame = CGRectMake(_lowerImageView.frame.origin.x, -location.y,
_lowerImageView.frame.size.width, _lowerImageView.frame.size.height);
[UIView animateWithDuration:.5 animations:^{
self.folderContentsView.frame = CGRectMake(0, location.y,
_lowerView.frame.size.width, 200);
self.lowerView.frame = CGRectOffset(_lowerView.frame, 0, 200);
}];
}
-(void) closeOverlay:(UITapGestureRecognizer*) sender
{
[UIView animateWithDuration:.5 animations:^{
self.lowerView.frame = CGRectOffset(_lowerView.frame, 0, -200);
self.folderContentsView.frame = CGRectMake(0, self.folderContentsView.frame.origin.y,
self.folderContentsView.frame.size.width, 0);
} completion:^(BOOL finished) {
[self.upperView setUserInteractionEnabled:YES];
[self.lowerView setUserInteractionEnabled:YES];
self.upperView.frame = self.startRect;
}];
}
#end
two views a equipped with the same background in layers. If a tap is detected, both views are resized so that the upper view ends at the tap's y-coordinate, while the lower view starts there. the UIImageView added to the lowerView is moved in the opposite direction, so that the images stays in place.
Now the lowerView slides down while a contentView is added and resized at the same speed.
As you can see no image rendering or other tough action is required. just UIView animations.

Yet another custom UIView subclass

I would like to create a custom class by subclassing the UIView that would look like the one below:
Oh and the text says the body view should be 80px tall, its 50px.
But I cant seem to get it right. I have the class header prepared like so:
#import <UIKit/UIKit.h>
#interface MessageView : UIView
#property (strong, nonatomic)UIImage *backgroundImage;
#property (strong, nonatomic)UITextView *messageView;
#property (strong, nonatomic)UILabel *titleLabel;
- (id)initWithBackgroundImage:(NSString*)background;
- (void)setTitle:(NSString*)titleMessage;
- (void)setBody:(NSString*)bodyMessage;
#end
Now I will create these views by calling initWithBackgroundImage where I will pass the background image file name. The example shows one of these images. How do I implement the init method so that init will create a body like on the example except the texts will be empty?
My futile attempt of the init method was this:
- (id)initWithBackgroundImage:(NSString*)background
{
self = [super init];//[super initWithFrame:CGRectMake(0, 0, 270, 100)];
if (self)
{
backgroundImage = [[UIImage imageNamed:background] retain];
messageView = [[UITextView alloc] initWithFrame:CGRectMake(10, 40, 250, 50)];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 20)];
}
return self;
}
But nothing happened. When done I will set the text via setter methods. And I would like to set the position on the final view via setCenter(x, y). I want the class to have "fixed" width and height.
Thanks!
EDIT:
- (id)initWithBackgroundImage:(NSString*)background
{
self = [super init];//[super initWithFrame:CGRectMake(0, 0, 270, 100)];
if (self)
{
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:background]];
messageView = [[UITextView alloc] initWithFrame:CGRectMake(10, 40, 250, 50)];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 20)];
[self addSubview:bg];
[bg release];
[self addSubview:messageView];
[messageView release];
[self addSubview:titleLabel];
[titleLabel release];
}
return self;
}

UiimageView adapted in a uiview

I have a little problem with my UIImageView.
I want the content of UIImageView don't overflow to UIView.
When I tryed this code, I have this result, but I won't a image in area striped.
UIView *view = [[UIView alloc] init];
[view setFrame:CGRectMake(10, 0, 300, 100)];
[self.view addSubview:view];
UIImageView *viewimageArticle = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
viewimageArticle.image =[UIImage imageNamed:#"article1.jpeg"];
viewimageArticle.contentMode = UIViewContentModeScaleAspectFill;
viewimageArticle.frame = CGRectMake(0, 0, 320, 100);
[view addSubview:viewimageArticle];
I tryed a many properties, "contentMode" and "autorezingMask" but i don't have good result.
Thank you !
Use
view.clipsToBounds = YES;
This causes its subviews to be clipped to the receivers bounds.
set clipsToBounds = YES on view
I am not 100% clear of what you want to achieve, but what you want may be UIViewContentModeScaleAspectFit instead of UIViewContentModeScaleAspectFill.
Also, you are creating the UIImageView frame in 300x100 first and resetting to 320x100. I believe the resetting part is not your intention.