Change UITextView with Floating Number Variable - ios

I want to display an UITextView on centre, dynamically both for 3.5 inch display and 4 inch display. but it has top and bottom 'padding' 20 pts.
So I create this code to measure UIView's height:
CGFloat screenHeight = _screen.frame.size.height;
float scrollHeight = screenHeight - 40;
then I add this line to set the screen :
_screen.frame = CGRectMake(20, 40, 320, scrollHeight);
but I see no difference on my 4 inch iPod touch... what did I do wrong?
UPDATE :
here's my .h file :
#property (strong, nonatomic) IBOutlet UIView *screen;
and here's my .m file :
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
_screen.frame = CGRectMake(20, 40, 320, screenHeight);
}

You can get the screen informations for sure like this:
screenWidth = [UIScreen mainScreen].bounds.size.width;
screenHeight = [UIScreen mainScreen].bounds.size.height;
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
You can calculate with these dynamically every height or width you want, and your views will got the right frame.
EDIT:
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
_screen.frame = CGRectMake(20, 40, 320, screenHeight);
}
this will make you the _screen will start at 40 and it will hang out at the bottom 40 too.
if you want to make a centered view make it like this:
_screen.frame = CGRectMake(20, 40, screenWidth-40, screenHeight-80);
and don't forget to add the screen to the superview like:
[self.view addSubview:_screen];
I hope it helps.
And one more tip: You can see your _screen frame, if you set _screen.backgroundcolor = [UIColor orangeColor]; for testing !

Related

code works fine on emulator but getting nil on real device

this code is working fine and i get button while i test this code on emulator but I get nil when I test the on real device.
button is declared in header file interface
#property (weak, nonatomic) UIButton *button;
then it is synthesized in implementation file
#synthesize button;
then in a function call i am doing this
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
screenHeight = screenHeight - 135;
//Call
button = [UIButton buttonWithType:UIButtonTypeCustom];
when i run this code on real device (iphone 5) the button is always nil. I have noticed while debugging that after running last line
the execution goes back to
CGFloat screenHeight = screenRect.size.height;
and starts again, by the way there is no loop code here.. I cant understand this phenomena. what am i missing here.
You have a weak reference to your button, so it's getting deallocated before you add it as a subview. You can either change it to have a strong reference:
#property (nonatomic, strong) UIButton *button;
Or keep a reference to the button while you add it to your view:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:myButton];
_button = myButton;
It is now retained by the view so won't get released.
code1 :
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
screenHeight = screenHeight - 135;
button = [UIButton buttonWithType:UIButtonTypeCustom];
code2:
CGRect rect = CGRectMake(10, 10, 10, 10);
CGFloat a = rect.size.width;
CGFloat b = rect.size.height;
b = b - 1;
button = [UIButton buttonWithType:UIButtonTypeCustom];
I run code1 and code2 in the device,I find that
code1 will go back to CGFloat screenHeight = screenRect.size.height;
and code2 will not go back after running last line.
I guess maybe something relation about the device efficiency or the function([[UIScreen mainScreen] bounds]) .
Hope it helps.

sizing a UIView embeded in a UIView

I am attempting to make a UIView that is constrained to the bottom of the container view without actually doing auto layout an constraints. Here is my code:
-(void)viewDidLoad
{
[super viewDidLoad];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
self.graphView.frame = CGRectMake(0,screenHeight - (screenWidth / 2), screenWidth, screenWidth / 2);
}
I was hoping this would create a subview that would appear pinned to the bottom of the screen and be the width of the screen and half that tall. However, when the view loads, there is some space between my graphView and the bottom of the screen. Any clue why this is?
I think you are using screenWidth when you need use screenHeight
The correct code:
-(void)viewDidLoad
{
[super viewDidLoad];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
self.graphView.frame = CGRectMake(0,screenHeight - (screenHeight / 2), screenWidth, screenHeight / 2);
}
The solution: Turn off auto layout and any other IB toggles that have an effect on where the view is being placed. The original code is correct for the rectangle I wanted, it was just being moved by auto layout. Also it's better to set up the graph container like this:
self.graphContainer = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight - (screenHeight / 2), screenWidth, screenHeight / 2)];

Change frame of UIView created in interface

My app was designed for iPhone 5, but now I'm adding compatibility for other devices.
I have a UIView, created in Interface Builder. I want to set a new frame for this view. I am checking the screen's width and height at runtime, but it's not changing the view's frame. Here is my code:
//in viewDidLoad:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
if (screenHeight == 480 && screenWidth == 320) {
bottomView.frame = CGRectMake(0, 60, 320, 420);
}
Goto interface builder tap on view, you want to resize, click on Attribute inspector than click on size and select freeform after that in your viewWillAppear resize your view, you can check size of you view by:-
NSLog(#"view frame ==> %#", NSStringFromCGRect(self.view.frame));
Make a class that returns you a screen frame info.
Create a new File with name FullScreen with UIScreen as sub class
#import <UIKit/UIKit.h>
#interface FullScreen : UIScreen
+ (CGRect)iOS7StyleScreenBounds;
#end
#import "FullScreen.h"
#implementation FullScreen
+ (CGRect)iOS7StyleScreenBounds {
UIScreen *screen = [UIScreen mainScreen]; CGRect screenRect;
if (![screen respondsToSelector:#selector(fixedCoordinateSpace)] && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
screenRect = CGRectMake(screen.bounds.origin.x, screen.bounds.origin.y, screen.bounds.size.height, screen.bounds.size.width);
} else {
screenRect = screen.bounds;
}
return screenRect;
}
#end
Now import this class in your view controller and call like this:
self.view.frame = [FullScreen iOS7StyleScreenBounds];
Try Putting the code in
- (void)viewDidAppear:(BOOL)animated;
One definite solution which can work is , you should remove your UIView from xib and create it programmatically.Which can be done by the following code:
newView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[newView setBackgroundColor:[UIColor yellowColor]];
[self.view newView];

UIScrollview doesn't scroll iOS

I'm creating several scroll views in one view that scroll horizontally. The following code works fine:
-(void)updateSection {
builder = [NSMutableArray array];
float xPosition = 0;
float xposbut = 100;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.frame.size.width, self.frame.size.height - 69)];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.delegate = self;
[self addSubview:scrollView];
for (int i = 1; i < itemArticleArray.count; i++) {
ItemView *item = [ItemView loadNibs];
item.frame = CGRectMake(xposbut, 10, item.frame.size.width, item.frame.size.height);
xPosition += scrollView.frame.size.width + 2;
xposbut += 500;
UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xposbut - 50, 4, 2, scrollView.frame.size.height - 8)];
[scrollView addSubview:seperatorView];
xPosition += scrollView.frame.size.width + 4;
[scrollView addSubview: item];
[builder addObject:item];
}
[scrollView setContentSize:CGSizeMake(xposbut, scrollView.frame.size.height)];
[self addSubview:scrollView];
}
However, when I change the 8th line of code to the following:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, self.frame.size.width, self.frame.size.height - 69)];
It cause the layout to look fine, but the scroll views do then not scroll at all. Any ideas?
you set the contentSize.height to the scrollView.frame.size.height and so the scrollView cant scroll. you have to set the contentSize to the total height of you scrollView, including the not visible area. The frame is only the area on screen.
UIScrollview will scroll when content size is bigger then frame size. In your code you set content size equal to frame size that's why your scrollview is not scrolling.
Set the content size for the scroll view. The content size should be greater than frame.size, then only the scroll view will scroll. And also enable scrolling.
Use the code
scrollView.scrollEnabled=YES;
scrollView.contentSize=CGSizeMake(CGFloat width, CGFloat height);
Height should be greater than scrollView.frame.size.height and add contents inside scrollview beyond the scrolview's frame.
You must give the scroll some space to work so check this out:
Create the scroll in the storyboard
Create an outlet for the scroll
And the in the .m file you must paste these lines
The code:
(void)viewDidLoad {
[super viewDidLoad];
yourScroll.scrollEnabled=YES;
yourScroll.contentSize=CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*2);
}
/*
* yourScroll.contentSize=CGSizeMake(your desired scroll width, your desired scroll height);
* The values must be bigger than the viewController size
*/

UIScrollview - how to make subview smaller as it scrolls off screen

I'm a beginner with iOS, so i'm just not sure what to research here. I have a UIScrollView with a few square subViews added. How can i make the subviews smaller as they scroll off screen and bigger as they approach the center of the screen?
#import "HorizontalScrollMenuViewController.h"
#import <UIKit/UIKit.h>
#define SUBVIEW_WIDTH_HEIGHT 280
#interface HorizontalScrollMenuViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
#end
#implementation HorizontalScrollMenuViewController
-(void)viewDidLoad{
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor greenColor],[UIColor redColor],[UIColor orangeColor],[UIColor blueColor],nil ];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGFloat originX = (screenWidth - SUBVIEW_WIDTH_HEIGHT)/2.0; // get margin to left and right of subview
CGFloat originY = ((screenHeight - SUBVIEW_WIDTH_HEIGHT)/2);
// add subviews of all activities
for (int i = 0; i < colors.count; i++){
CGRect frame = CGRectMake(0,0,SUBVIEW_WIDTH_HEIGHT,SUBVIEW_WIDTH_HEIGHT);
frame.origin.x = self.scrollView.frame.size.width * i + originX;
frame.origin.y = originY;
UIView *subView = [[UIView alloc] initWithFrame:frame];
[UIView setAnimationBeginsFromCurrentState: YES];
subView.layer.cornerRadius = 15;
subView.layer.masksToBounds = YES;
subView.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
}
#end
Here you can find a fully working example of what you're trying to accomplish. It only has
one subview because it's just to give you an idea of how can you accomplish it. Also, this example was tested on an iPad (iOS7) simulator.
The *.h file
#import <UIKit/UIKit.h>
// Remember to declare ourselves as the scroll view delegate
#interface TSViewController : UIViewController <UIScrollViewDelegate>
#property (nonatomic, strong) UIView *squareView;
#end
The *.m file
#import "TSViewController.h"
#implementation TSViewController
#synthesize squareView = _squareView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Create and configure the scroll view (light gray)
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 100, 500, 500)];
CGRect contentSize = myScrollView.frame;
contentSize.size.height = contentSize.size.height + 400;
myScrollView.contentSize = contentSize.size;
myScrollView.userInteractionEnabled = YES;
// give the scroll view a gray color so it's easily identifiable
myScrollView.backgroundColor = [UIColor lightGrayColor];
// remember to set yourself as the delegate of the scroll view
myScrollView.delegate = self;
[self.view addSubview:myScrollView];
// Create and configure the square view (blue)
self.squareView = [[UIView alloc] initWithFrame:CGRectMake(200, 400, 60, 60)];
self.squareView.backgroundColor = [UIColor blueColor];
[myScrollView addSubview:self.squareView];
}
// Here is where all the work happens
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Get the difference between the contentOffset y position and the squareView y position
CGFloat y = self.squareView.frame.origin.y - scrollView.contentOffset.y;
// If the square has gone out of view, return
if (y <= 0) {
return;
}
// Modify the squareView's frame depending on it's current position
CGRect squareViewFrame = self.squareView.frame;
squareViewFrame.size.height = y + 5;
squareViewFrame.size.width = y + 5;
squareViewFrame.origin.x = (scrollView.contentSize.width - squareViewFrame.size.width) / 2.0;
self.squareView.frame = squareViewFrame;
}
#end
And here is a little explanation of what is going on:
A UIScrollView has several properties that allow you to configure it correctly. For example it has a frame (gray) which is inherited from UIView; with this property you specify the visible size of the scroll view. It also has the contentSize (red) which specifies the total size of the scroll view; in the image it's showed as the red area but this is only for illustration purposes as it will not be visible in the program. Imagine the scroll view's frame as the window that let's you see only a part of the bigger content the scroll view has.
When the user starts scrolling a gap appears between the top part of the contentSize and the top part of the frame. This gap is known as the contentOffset
Here is the reference to UIScrollView
Here is the reference to UIScrollViewDelegate
Hope this helps!
Assuming that you have the scrollView inside self.view, you can implement scrollViewDidScroll: in the scroll view delegate to find when it is scrolled.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
for (UIView *view in self.scrollView.subviews) {
CGRect frame = [view convertRect:view.frame toView:self.view]; // Contains the frame of view with respect to self.view
}
}
You can them use the frame to resize subviews as you want.
The answer starts with analyzing the UIScrollView Class Reference and it's delegate. In the delegate documentation see the responding to scrolling and dragging section. You should also review the sample code for each. You can create outlets to your subviews and change the subview properties within a uiview animation. These references will give you a good foundation in understanding where you can build the call to animate the subviews.
Here is a link to animating subviews. Additional examples can be found by Googling "uiview subview animation" (without the quotes). If you run into any major issues read the header files first and post some sample code for additional (more precise) help.
Other reference:
UIKit ScrollViews

Resources