Place a label on the bottom of the screen - ios

I made the following code to get a label on the bottom of the screen, depending if the device is a iphone5 or iphone4/4s.
I like to know I there is another way.... for instance with auto-layout?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
CGRect frame;
if ([UIScreen mainScreen].bounds.size.height == 568) {
frame = CGRectMake(140, 500, 320, 100);
} else {
frame = CGRectMake(140, 410, 320, 100);
}
UILabel *iconAdLabel = [[UILabel alloc]init];
iconAdLabel.frame = frame;
iconAdLabel.font = [[UIFont preferredFontForTextStyle:UIFontTextStyleBody] fontWithSize:11.0];
iconAdLabel.text = #"SOME NICE TEXT HERE";
[self.view addSubview:iconAdLabel];
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UILabel *iconAdLabel = [[UILabel alloc]init];
iconAdLabel.frame = CGRectMake(0,self.view.frame.size.height-100,320,100);
iconAdLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:iconAdLabel];
}

Yes indeed. You definitely DO NOT want to hard code in position values. Use interface builder to add constraints to the label. Apple's documentation on Auto-layout is quite good and can give you ideas as well.

Instead of hardcoding values or using constraints and interface builder, you could use a simple math expression like so:
CGFloat const labelHeight = 100;
CGRect frame = CGRectMake(0, CGRectGetHeight([[UIScreen mainScreen] bounds]) - labelHeight, 320, labelHeight);

Related

Making a list of UIViews that slide up and down when touched

I'm trying to figure out an approach to build something like the image below, which is a list of items that when a section is clicked slides out content. It's a really common UX on most websites and what not. My idea is to have each gray box (button) slide out a UIView containing some other items. I'm still new to iOS development but I'm struggling to find how you can animate a UIView to slide down and push the content below it down as well. Hoping some one can give me a good starting point or point to some info outside the realm of the apple docs.
Thanks!
So if you just have a few views, I would not recommend the UITableView approach, since it is not so easy to customize with animations and table views usually want to fill the whole screen with cells. Instead write a expandable UIView subclass that has the desired two states. Add a method to switch between extended and collapsed state. On expanding/collapsing adjust their positions so that they always have enough space.
I provide you an example of views adjusting their frames. I guess it should be easy to do the same with auto layout constraints: give the views a fixed height constraint and change this on collapsing/expanding. The same way set the constraints between the views to be 0 so that they are stacked on top of each other.
Expandable View:
#interface ExpandingView(){
UIView *_expandedView;
UIView *_seperatorView;
BOOL _expanded;
}
#end
#implementation ExpandingView
- (id)init
{
self = [super initWithFrame:CGRectMake(15, 0, 290, 50)];
if (self) {
_expanded = NO;
self.clipsToBounds = YES;
_headerView = [[UIView alloc] initWithFrame:self.bounds];
_headerView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
[self addSubview:_headerView];
_seperatorView = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-1, self.bounds.size.width, 1)];
_seperatorView.backgroundColor = [UIColor lightGrayColor];
[self addSubview:_seperatorView];
_expandedView = [[UIView alloc] initWithFrame:CGRectOffset(self.bounds, 0, self.bounds.size.height)];
_expandedView.backgroundColor = [UIColor blueColor];
[self addSubview:_expandedView];
}
return self;
}
- (void)layoutSubviews{
[self adjustLayout];
}
- (void)adjustLayout{
_headerView.frame = CGRectMake(0, 0, self.bounds.size.width, 50);
_seperatorView.frame = CGRectMake(0, 49, self.bounds.size.width, 1);
_expandedView.frame = CGRectMake(0, 50, self.bounds.size.width, self.bounds.size.height-50);
}
- (void)toggleExpandedState{
_expanded = !_expanded;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, _expanded?200:50);
[self adjustLayout];
}
#end
ViewController:
#interface ExpandingViewController (){
NSArray *_expandingViews;
}
#end
#implementation ExpandingViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_expandingViews = #[
[[ExpandingView alloc] init],
[[ExpandingView alloc] init],
[[ExpandingView alloc] init],
];
for(ExpandingView *view in _expandingViews){
[view.headerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(expandingViewTapped:)]];
[self.view addSubview:view];
}
}
- (void)viewWillLayoutSubviews{
int y = 100;
for(ExpandingView *view in _expandingViews){
view.frame = CGRectOffset(view.bounds, (CGRectGetWidth(self.view.bounds)-CGRectGetWidth(view.bounds))/2, y);
y+=view.frame.size.height;
}
}
- (void)expandingViewTapped:(UITapGestureRecognizer*)tapper{
ExpandingView *view = (ExpandingView*)tapper.view.superview;
[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0 options:0 animations:^{
[view toggleExpandedState];
[self.view layoutIfNeeded];
} completion:nil];
}

PEPhotoCropEditor cropview.croprect being ignored

Using PEPhotoCropEditor I'm trying to set cropview.croprect but the rect seem to ignore it and still calculate the rect based on the image size. Meanwhile .cropRect works well if I use a controller to access it.
What I'm trying to do is create a fix cropview regardless of what image is selected.
Here's my code:
cropView = [[PECropView alloc] initWithFrame:self.view.bounds];
cropView.image = coverPhotoView.image;
cropView.cropRect = CGRectMake(0, 0, 320, 173);
[self.view addSubview:cropView];
Place your initialisation code into ViewDidLoad:
- (void)viewDidLoad
{
cropView = [[PECropView alloc] initWithFrame:self.view.bounds];
cropView.image = coverPhotoView.image;
[self.view addSubview:cropView];
}
And then set your CropRect in ViewDidAppear:
- (void)viewDidAppear:(BOOL)animated
{
cropView.cropRect = CGRectMake(0, 0, 320, 173);
}

UIScrollView does white space and doesn't scroll

I have a scenario in mind, which I tried to implement but to no avail- Put a scrolling view with a text box, all of that within page controller.
Here's what I've done- UIPageViewController->ViewController with data ->UIView ->UIScrollView -> UITextView.
What it does? Well, other than scrolling a bit (not to show all of the text) and showing a weird white bar at the bottom, nothing.
Useful snippets of code- basically I linked my height constraint, and then in code:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//CGSize sizeThatShouldFitTheContent = [_textView sizeThatFits:_textView.frame.size];
self.theScrollView.frame = CGRectMake(self.theScrollView.frame.origin.x, self.theScrollView.frame.origin.y, 320, 2000);//[_theScrollView sizeThatFits:_theScrollView.frame.size];
_heightOfText.constant = 2000; //sizeThatShouldFitTheContent.height;
//_textView.frame = CGRectMake(_textView.frame.origin.x, _textView.frame.origin.y, sizeThatShouldFitTheContent.width, sizeThatShouldFitTheContent.height);
_textView.scrollEnabled = NO;
NSLog(NSStringFromCGRect(self.theScrollView.frame));
self.theScrollView.scrollEnabled = YES;
}
But nope.
Thanks in advance
This solved my issue- apparently I should call a selector...
-(void)viewDidLoad
{
// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height + 40);
[self performSelector:#selector(adjustScrollViewContentSize) withObject:nil afterDelay:0.1];
}
-(void)adjustScrollViewContentSize
{
_scrollView.contentSize = CGSizeMake(4000, 4000);
}

Centering a child UIView to its parent UIView

I want to center a subview in the middle of its parent, I have looked at answers on SO but so far they have not helped me. This one specifically looks like it should work, but doesn't.
Here is what I am doing
-(void)viewDidLoad {
[super viewDidLoad];
finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
finalNumberCircle.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
finalNumberCircle.Color = [UIColor darkGrayColor];
[self.view addSubview:finalNumberCircle];
}
I also tried the following:
-(void)viewDidLoad {
[super viewDidLoad];
CGPoint translatedP = [self.view convertPoint:self.view.center fromView:self.view.superview];
finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100];
finalNumberCircle.center = translatedP;
[self.view addSubView:finalNumberCircle];
}
Here is how it looks (it's the grey circle)
You have to move your code (2. example) from viewDidLoad to viewWillLayoutSubviews and it will work as suspected.
Before viewWillLayoutSubview is called your viewcontroller view bounds still can change.
convertPoint and related methods used when you want to convert coordinates from one view's coordinates to another, that's not your situation. You have to do something like:
finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
finalNumberCircle.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
[self.view addSubview:finalNumberCircle];
Try below code...
[self.view addSubView:finalNumberCircle];
finalNumberCircle.frame=CGRectMake(finalNumberCircle.frame.origin.x+(self.view.frame.size.width/2-finalNumberCircle.center.x), finalNumberCircle.frame.origin.y+(self.view.frame.size.height/2-finalNumberCircle.center.y), finalNumberCircle.frame.size.width, finalNumberCircle.frame.size.height);
Another possibility how to do it (in case You need specific offset from sides). Just provide an offset from parent view sides, and it will auto-resize itself in center of parent view, and will also autoresize, in case parent view frame is changed.
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat mOffsetFromSide = 50;
finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x+mOffsetFromSide, self.view.bounds.origin.y+mOffsetFromSide, self.view.bounds.size.width-mOffsetFromSide*2, self.view.bounds.size.height-mOffsetFromSide*2];
[finalNumberCircle setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
[self.view addSubView:finalNumberCircle];
}
In case You simply want to position view in it's parent view's center, leaving it's frame intact, then:
finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
finalNumberCircle.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
[self.view addSubview:finalNumberCircle];
And yes - it is exact copy of Your provided code. It works on my side.
Maybe problem is with CircleView itself. Or You change it's frame later in code?
What happens if You change "CircleView" to simply UIView ?
I think that you should move half width to the left, and half height upwards:
-(void)viewDidLoad {
[super viewDidLoad];
finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(self.view.center.x-50, self.view.center.y-50, 100, 100];
[self.view addSubView:finalNumberCircle];
}
But I would use something like:
#DEFINE CIRCLE_RADIUS 50
-(void)viewDidLoad {
[super viewDidLoad];
finalNumberCircle = [[CircleView alloc] initWithFrame:CGRectMake(self.view.center.x-CIRCLE_RADIUS, self.view.center.y-CIRCLE_RADIUS, 2*CIRCLE_RADIUS, 2*CIRCLE_RADIUS];
[self.view addSubView:finalNumberCircle];
}
I uploaded a demo https://github.com/luisespinoza/CenterViewTest

UIView animate alpha when view is offscreen doesn't work

I have a UIView which is located offscreen and I'm animating the frame so that the view slides in offscreen from the bottom and is visible. I'd like to simultaneously animate the alpha property of a UILabel on the view as well so it fades in. Unfortunately it appears I can't do the alpha animation because the view is offscreen and doesn't appear to take hold. It looks something like this:
nextCell.titleLabel.alpha = 0;
[UIView animateWithDuration:collapsedAnimationDuration animations:^{
CGRect newFrame = lastCell.frame;
newFrame.origin = CGPointMake(lastCell.frame.origin.x , lastCell.frame.origin.y + THREAD_CELL_HEIGHT);
nextCell.frame = newFrame;
nextCell.titleLabel.alpha = 1;
}];
Is it not possible to start animating the alpha of the subview because it's offscreen? If I position the view on screen and then try the animation it looks great but that's not the effect I'm going for. Thanks for your help.
Is this code executed in cellForRowAtIndexPath? If so, try moving it to tableView:willDisplayCell:forRowAtIndexPath:. The table view resets various properties of the cell before displaying it.
From the AppDelegate didFinishLaunching method:
self.myView = [[MyView alloc] initWithFrame:CGRectMake(320, 480, 400, 400)];
self.myView.titleLabel.text = #"test text";
self.myView.titleLabel.alpha = 0;
[UIView animateWithDuration:10.0 animations:^{
CGRect newFrame = self.myView.frame;
newFrame.origin = CGPointMake(0 , 0);
self.myView.frame = newFrame;
self.myView.titleLabel.alpha = 1;
}];
[self.viewController.view addSubview:self.myView];
MyView is just this:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self addSubview:self.titleLabel];
}
return self;
}
- (UILabel *)titleLabel
{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
}
return _titleLabel;
}
I did no important changes to the code you presented and it worked fine. So assuming you're not doing what Tim mentioned (it won't work if you're doing it), we need more details to help you out.

Resources