Synchronous changing and moving UIView - ios

If I'm doing at the same time
CGRect centView = rootview.frame;
centView.origin.x = rootview.frame.origin.x;
centView.origin.y = your_float;
centView.size.height = screenHeight - (your_float * 2);
rootview.frame = centView;
and
CGRect frame = myview.frame;
frame.origin.x = myview.frame.origin.x;
frame.origin.y = 20 - shift - 3;
myview.frame = frame;
and
myview is be in rootview then myview not moving and moving jerkily. How do they work together?

Related

view frame does not move down 20px (height of the statusbar)

I used the code below to present vViewController1
#property (retain,nonatomic) ViewController1 *vViewController1;
...
push vViewController1 from rootViewController
[self.navigationController pushViewController:vViewController1 animated:NO];
Viewcontroller1's viewWillAppear
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
CGRect screen = [[UIScreen mainScreen] bounds];
if (self.navigationController) {
CGRect frame = self.navigationController.view.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
self.navigationController.view.frame = frame;
} else {
if ([self respondsToSelector: #selector(containerView)]) {
UIView *containerView = (UIView *)[self performSelector: #selector(containerView)];
CGRect frame = containerView.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
containerView.frame = frame;
} else {
CGRect frame = self.view.frame;
frame.origin.y = 20;
frame.size.height = screen.size.height - 20;
self.view.frame = frame;
}
}
}
}
but it displays as below and does not move down 20px(height of the statusbar)
Your comment welcome
Assuming you are NOT using Autolayout (seeing as you are playing with frame sizes) then in Storyboard select the ViewController and then goto the Size Inspector (little ruler) and then adjust the Y delta for the view (change to 20 or -20 depending on what mode you are "View As").
This may help here although I am not sure about the 18 value in Y I always thought it was 20!

iOS, Graceful Orientation Change

I call this function at viewDidLoad and didRotateFromInterfaceOrientation
-(void) makeBox{
[self.view1 removeFromSuperview];
float viewWidth = CGRectGetWidth(self.view.frame);
float viewHeight = CGRectGetHeight(self.view.frame);
float startx = (viewWidth / 100) * 10;
float starty = (viewHeight /100) * 20;
float width = (viewWidth / 100) * 80;
float height = (viewHeight /100) * 60;
CGRect frame1 = CGRectMake(startx, starty, width, height);
self.view1 = [[UIView alloc] initWithFrame:frame1];
[self.view1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:self.view1];
}
When the view does change, its very 'sketchy' & not very graceful at all. I'm using simulator but i assume that it would be the same if i were to run on a device. How would i go about making this transition smoother? I would post to user experiance page, but i wish todo this programmatically
The overall point of this, other than to learn, is to achieve orientation independent graphics from code (without autolayout).
There's no need to remove the view and create a new one with the desired frame if its the only thing you're changing. Just modify the view frame in-place:
- (void)makeBox {
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
CGFloat viewHeight = CGRectGetHeight(self.view.frame);
CGFloat startx = (viewWidth / 100) * 10;
CGFloat starty = (viewHeight /100) * 20;
CGFloat width = (viewWidth / 100) * 80;
CGFloat height = (viewHeight /100) * 60;
CGRect view1Frame = CGRectMake(startx, starty, width, height);
if (!self.view1) {
// The view doesn't exists yet, we create it
self.view1 = [[UIView alloc] initWithFrame:view1Frame];
[self.view1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:self.view1];
}
else {
// Just update the frame
self.view1.frame = view1Frame;
}
}
For extra-nice UX, wrap the frame update in an animation block:
// ... snip ...
else {
[UIView animateWithDuration:0.3 animations:^() {
self.view1.frame = view1Frame;
}];
}

scrollRectToVisible not working in page view controller - iPhone sdks

I am using Xcode 6.1 and developing for iOS 8.0.
In my application I have a UIPageViewController. In that I have added a UIScrollView. In the UIScrollView I have 4 buttons. When I tap on a button I use this code.
long btnValue = sender.tag;
float spaceing = 0;
if(btnValue == 1)
spaceing = self.scrollView.frame.size.width/4*btnValue;
else
spaceing = (self.scrollView.frame.size.width/2*btnValue) - self.scrollView.frame.size.width/4;
CGRect frame;
frame.origin.x = spaceing;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
self.scrollView.pagingEnabled = NO;
This(scrollRectToVisible:) is not working. Can anyone help me?
Thanks.
long btnValue = sender.tag;
float spaceing = 0;
if(btnValue == 1)
spaceing = self.scrollView.frame.size.width/4*btnValue;
else
spaceing = (self.scrollView.frame.size.width/2*btnValue) - self.scrollView.frame.size.width/4;
CGRect frame;
frame.origin.x = spaceing;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
self.scrollView.pagingEnabled = NO;
// Problem is here
frame.size = self.scrollView.frame.size; //scrollView.frame.size matters
set
frame.size = self.View.frame.size;
and if you are doing it by coding then please add your scrollview into your view
[self.view addSubview:yourScrollView];

change subviews according to its superview frame

I have a custom UIView class
CustomView.m
- (instancetype)initWithString:(NSString *)aString {
self = [super init];
if (!self)
return nil;
[self setAutoresizesSubviews:YES];
UILabel *label = [[UILabel alloc] init];
label.frame = ({
CGRect frame = self.frame;
frame.origin.x = CGRectGetMinX(frame) + 50;
frame.origin.y = CGRectGetMinY(frame) + 20;
frame.size.width = CGRectGetWidth(frame) - 100;
frame.size.height = CGRectGetHeight(frame) - 40;
frame;
});
[label setText:aString];
[self addSubview:label];
return self;
}
ViewController.m
-(void)addCustomView {
CustomView *custom = [CustomView alloc] initWithString:#"abc"];
custom.frame = ({
CGRect frame = self.view.frame;
frame.origin.y = CGRectGetHeight(frame) - 100;
frame.size.height = 100;
frame;
});
[self.view addSubview: custom];
}
Here's the problem, I set frame of my CustomView after alloc init it.
Which means its frame is CGRectZero before I change it.
Therefore, the frame of UILabel is zero, too.
How can I change frame of my subviews after their superview's frame changed ? Like above.
Thanks.
You simply need to implement -layoutSubviews in your CustomView, and do your subview layout/frame calculation stuff in there. -layoutSubviews will get called when your CustomView's frame changes.
Edit for further clarification:
You could add a property for the label, or use a tag, so that you can access it it -layoutSubviews, something like this;
- (void)layoutSubviews
{
self.label.frame = ({
CGRect frame = self.frame;
frame.origin.x = CGRectGetMinX(frame) + 50;
frame.origin.y = CGRectGetMinY(frame) + 20;
frame.size.width = CGRectGetWidth(frame) - 100;
frame.size.height = CGRectGetHeight(frame) - 40;
frame;
});
}
Edit 2:
Of course, what you probably want to do as well is change your initialiser to include the frame, like so:
- (instancetype)initWithFrame:(CGRect)frame string:(NSString *)string
{
self = [super initWithFrame:frame];
if (self) {
// Do some stuff
}
return self;
}
That way, your view's frame is set when you first create the label. Implementing -layoutSubviews is still a good idea though, for any future frame changes/orientation changes etc.

Set position of UIPickerView at bottom even after scroll in ios

I am trying to show UIPickerView on click on UITextField. I am using a simple method to show the picker as follows:
-(void) showPickerView {
[self.view resignFirstResponder];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.50];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if(iOSDeviceScreenSize.height == 480){
CGRect frame = self.picker.frame;
frame.origin.y = 270;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 226.0;
self.toolBar.frame = frame;
} else if(iOSDeviceScreenSize.height == 568){
CGRect frame = self.picker.frame;
frame.origin.y = 239.0;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 195.0;
self.toolBar.frame = frame;
}
[UIView commitAnimations];
}
It works great if I have not scrolled down. But when I scroll down, my picker is placed on fix position as mentioned in showPickerView method. Now how I can manage it that my picker appears at bottom every time.
Solution 1:
Add your pickerview to your window.
Solution 2:
Implement the scrollview delegate and adjust the frame continuously.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if(iOSDeviceScreenSize.height == 480){
CGRect frame = self.picker.frame;
frame.origin.y = 270;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 226.0;
self.toolBar.frame = frame;
} else if(iOSDeviceScreenSize.height == 568){
CGRect frame = self.picker.frame;
frame.origin.y = 239.0;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 195.0;
self.toolBar.frame = frame;
}
}
I can just suppose that you are showing your picker inside of a scroll view, right?
And if so, then your picker frame is relative to UIScrollView, not to a screen view.
So you have to take into account your scroll view offset. Something like that:
CGRect frame = self.picker.frame;
frame.origin.y = self.scrollvew.contentOffset.y + 270;
Otherwise you can show your picker outside of the UIScrollView (just add it to a screen view).

Resources