get x y location of a textbox inside scrollview - ios

The layout of my UIViewController is :
Layer 1:UIView (height is 568)
Layer 2:UIScrollView (height is 2000)
Layer 3:UIView (height is 2000)
i have lot of textfield inside this layer 3 UIView, i would like to know the current x y showing on layer1 UIView (not the layer 3 UIView original x y)
should i use convertPoint ? I got invalid results
-(void)textFieldDidBeginEditing:(UITextField *)textField {
CGPoint newPoint = [textField convertPoint:textField.frame.origin fromView:self.view];
NSLog(#"%f",textField.frame.origin.y);
NSLog(#"%f", newPoint.y);
}

You have to create IBOutlet for your Layer 1 view and Layer 3 view.
in your ViewController.h
#property (strong, nonatomic) IBOutlet UIView *superView;
#property (strong, nonatomic) IBOutlet UIView *subView;
in your ViewController.m
CGPoint originInSuperview = [self.superView convertPoint:textField.frame.origin fromView:self.subView];
Then you will have the SuperView x y location.

You can use superview means a view which holds the another view over it. It manages subviews. Subviews means the views which are holding/added over a 'View'.
Lets Say. you have a SuperView UIview which has a subview UIScrollView and which has another subview UIview over it.
Might be it helps you. The event-handling methods: OnTouch

UITextField *myTextField;
Get textFiled origin x and Y
myTextField.frame.origin.x;
myTextField.frame.origin.y;
Get Minmum(origin) and maximum (end) and mid point of textFiled
CGRectGetMinX(myTextField.frame);
CGRectGetMaxX(myTextField.frame);
CGRectGetMinY(myTextField.frame);
CGRectGetMaxY(myTextField.frame);
CGRectGetMidX(myTextField.frame);
CGRectGetMidY(myTextField.frame);

Related

Change height of view programmatically in uistackview

I need to change the view height in the stack view when I press the test button, but it is not working properly.
When I press the test button, I want to set the height of view 3 to 50 and the height of view5 to fill the remaining area. When I press the test button again, i want to reverse to process. How can I do that?
Thank you.
As #SeanLintern88 mentioned, the way you really should be doing this is with auto layout constraints -- you don't want to be mixing setFrame with autolayout.
IBOutlet the height constraints for View 3 and View 5. Set the View 3 height constraint as inactive to start (if you want it to look like your storyboard does currently to start), then whenever the button is pressed, check which constraint is active and flip-flop them.
#import "ViewController.h"
#interface ViewController ()
#property (strong, nullable) IBOutlet NSLayoutConstraint *view3HeightConstraint;
#property (strong, nullable) IBOutlet NSLayoutConstraint *view5HeightConstraint;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// start us out as inactive
self.view3HeightConstraint.active = NO;
}
- (IBAction)btnPressed:(id)sender {
if (self.view5HeightConstraint.active) {
// view 5 height constraint is active
// you can set the height constants directly in storyboard as well
self.view3HeightConstraint.constant = 50.0f;
self.view3HeightConstraint.active = YES;
self.view5HeightConstraint.active = NO;
} else {
// view 3 is height constraint is active
// you can set the height constants directly in storyboard as well
self.view5HeightConstraint.constant = 50.0f;
self.view5HeightConstraint.active = YES;
self.view3HeightConstraint.active = NO;
}
// animate the layoutIfNeeded so we can get a smooth animation transition
[UIView animateWithDuration:1.0f animations:^{
[self.view layoutIfNeeded];
}];
}
#end

Ios Animation of UIImageView with Constraints

I have an UIImageView which I placed in the center of screen using Align Center X and Align Center Y constraints.I want the image to move with animation to the top of screen keep the horizontal alignment in container and have a space of 20 from the top when i press a button. Remove the Align Center Y constraint programmatically and add the top space constraint is a good approach for that?
Calculate dynamically the constant with the UIScreen class and your view height.
#interface ViewController : UIViewController
#property (nonatomic, weak) IBOutlet NSLayoutConstraint *centerYConstraint;
#property (nonatomic, weak) IBOutlet UIView *myView;
- (IBAction)moveView:(id)sender;
#end
- (IBAction)moveView:(id)sender
{
self.centerYConstraint.constant = 20.0 + (self.myView.frame.size.height * 0.5) - ([UIScreen mainScreen].bounds.size.height * 0.5);
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
}];
}
You can download the code here (http://cl.ly/3l1B0k1L0h1C)

Resize xib UIView to fit UIButton inside

I have a UIView created from a xib file. Inside that UIView there is a UIButton with a text that may be longer or shorter depending on the language.
What I want is to resize the parent UIView to fit the width of the UIButton. How can I do it with AutoLayout, or programmatically?
Thanks,
add leading, trailing, top and bottom constraint of button
add width constraint of view and connect to viewWidthConstraint.
calculate width of button for language and set constant of viewWidthConstraint.
View and button will resize
#property (nonatomic, weak) IBOutlet NSLayoutConstraints *viewWidthConstraint;
- (void)viewDidLoad {
super.viewDidLoad();
self.viewWidthConstraint.constant = [self buttonWidth];
}
- (CGFloat)buttonWidth {
return 100.f; (calculate width of button)
}

Vertical Progress bar in iOS with Autolayout

I am trying to make a vertical progress bar in iOS that can be placed as a UIView in interface builder, and given a progress bar view class. Here are my class files:
ProgressBar.h:
#interface ProgressBar : UIView
#property (nonatomic, retain) IBOutlet UIView *barView
-(void)setBarValue:(float)value;
#end
ProgressBar.m:
-(id)initWithCoder:(NSCoder *)aDecoder {
id s = [super initWithCoder:aDecoder];
if (s) {
self.backgroundColor = [UIColor clearColor];
}
return s;
}
-(void)setBarValue:(float)val {
[self.barView setBackgroudnColor:TURQUOISE];
CGRect frame = self.barView.frame;
frame.size.height = self.barview.frame.size.height * val;
[self.barView setFrame:frame];
}
The only constraint I have on the 'barView' inside the ProgressBar element is 'align bottom edges'. The setFrame method never seems to update the bar, and even at full height, the inner self.barView isn't the same height as the ProgressBar view. The properties are all correctly linked in the Storyboard.
Any ideas?
The key issue is that, if using autolayout, you should add your constraints for this subview in IB, too. (I assume you already added constraints for the ProgressView.) And you should not change the frame of barView, but rather change its constraints and then call setNeedsLayout. The easiest way will be to add leading/trailing/top constraints of zero and then create a final height constraint. You can then add an IBOutlet for that height constraint. You can then programmatically set the constant of that constraint (and if you're going to use a multiplicative factor, it should be the product of this factor and the overall progress view, not a factor of the bar, itself), and then call setNeedsLayout.

How can I resize UIView width when some view hidden?

The above Image is captured from storyboard.
It is constructed like as follows:
UIView (include UILabel and UIProgressView) <--2px spacing --> UIActivityIndicatorView <---6px spacing ---> UIButton
I want the UIView to increase its width to button when UIActivityIndicatorView hidden as follows:
UIView( include UILabel and UIProgressView) <---6 space---> UIButton
How can I do that?? Let me Know. Please.
https://github.com/bilobatum/ActivityIndicatorDemo
Make the button's intrinsic content size along the horizontal axis required (i.e., priority 1000). This prevents the layout from being ambiguous along the horizontal axis.
#interface ViewController ()
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *activityIndicatorWidthConstraint;
#property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
// spacerConstraint is the horizontal spacer constraint between the activity indicator and the gray view
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *spacerConstraint;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.activityIndicator startAnimating];
}
- (IBAction)stopButtonTapped:(UIButton *)sender
{
sender.enabled = NO;
self.activityIndicatorWidthConstraint.constant = 0;
self.spacerConstraint.constant = 0;
[UIView animateWithDuration:1.0 animations:^{
self.activityIndicator.alpha = 0.0;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
[self.activityIndicator stopAnimating];
//[NSLayoutConstraint reportAmbiguity:nil];
}];
}
#end
If you are using Auto-layout, consider setting the width of the UIActivityIndicatorView to 0 when you disable it. This won't be exactly what you need, but it's close.
Let's assume that your UILabel is called _label, UIProgressView is called _progressView and UIActivityIndicator is called _activityIndicator.
When you hide your _activityIndicator, call this block of code:
[_label setFrame:CGRectMake(_label.frame.origin.x, _label.frame.origin.y, _label.frame.size.width + _activityIndicator.frame.size.width + 2, _label.frame.size.height)];
[_progressView setFrame:CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, _progressView.frame.size.width + _activityIndicator.frame.size.width + 2, _progressView.frame.size.height)];
When _activityIndicator shows again:
[_label setFrame:CGRectMake(_label.frame.origin.x, _label.frame.origin.y, _label.frame.size.width - _activityIndicator.frame.size.width - 2, _label.frame.size.height)];
[_progressView setFrame:CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, _progressView.frame.size.width - _activityIndicator.frame.size.width - 2, _progressView.frame.size.height)];
Remember, that Autolayout should be turned off, if you want to manually set the frames! It is very simple: just uncheck the "Use Autolayout" feature in File Inspector:

Resources