How to adjust Scrollview content size using autolayouts - ios

Hi in my project i have added scrollview on viewcontroller and inside that scrollview i have added textfields and buttons
After that i have added auto-layouts for all fields as like below image there i have added bottom space container is "58"
but when i clicked on textfield i have changed scrollview content size like below and fine that's ok
- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
[self.mainscrollview setContentSize:CGSizeMake(100, 700)];
}
here my main intention is when i clicked keyboard return button i want to set scroll view content size as like previous what i have applied
for this i have written some code but this showing exceptions please help me
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *bottomSpaceContraint;
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.mainscrollview setContentSize:(100, bottomContraint.size.height)];
return YES;
}

Your question is not really clear, but if you want to adjust the scroll view based on the presence of the keyboard use the awesome framework TPKeyboardAvoiding. Also, you have tagged your question with swift, but the code you provided is objc. You might wanna change the tag.

Related

Buttons/Links under view object in .xib file are not responding

Can somebody tell me why none of the buttons under the "License Agreement View" UIView object in my .xib file are triggering an action? It seems that the Scrollview object is messing up the behavior. When the buttons are directly under the "Scroll View" object (as opposed to the "License Agreement View" UIView object), then they function properly. But, I need to group my buttons under the UIViews as shown in the view hierarchy below.
Here's the view layout:
Here's my view hierarchy:
Here's the corresponding .m file:
#interface MYViewController ()
- (IBAction)licenseAgreementPressed:(id)sender;
- (IBAction)legalDisclaimerPressed:(id)sender;
- (IBAction)privacyStatementPressed:(id)sender;
#property (strong, nonatomic) IBOutlet UIView *licenseAgreementView;
#property (weak, nonatomic) IBOutlet UIButton *legalDisclaimerButton;
#property (strong, nonatomic) IBOutlet UIButton *privacyStatementButton;
#end
#implementation MYViewController
- (IBAction)licenseAgreementPressed:(id)sender
{
NSLog(#"Pressed A");
}
- (IBAction)legalDisclaimerPressed:(id)sender
{
NSLog(#"Pressed B");
}
- (IBAction)privacyStatementPressed:(id)sender
{
NSLog(#"Pressed C");
}
#end
Make sure you have made connection between IBAction(code) and UIButton(in xib) , please check this post How do I name and link an IBAction button created in a storyboard
Did you connect your actions to the elements in interface builder by control-dragging it?
If your actions are connected you will see a little filled out circle on the left of the line in your editor.
Check this tutorial for further information on how to connect your storyboard elements to your code.
It is because none of the buttons are inside the bounds of the license agreement view. A button outside its superview's bounds is untappable.
So the problem was the links, buttons, and text fields were not actually located within the bounds specified the parent views because the original .xib file didn't have the required constraints pinned to those elements. I discovered this by checking the "clip to bounds" checkbox in the Attributes Inspector pane. Whenever "clip to bounds" was checked, the app was NOT displaying any of the above view objects; I could only see links, buttons, and text fields when "clip to bounds" was unchecked but unfortunately, those elements were not clickable at that point. After setting the necessary constraints, however, the view elements were correctly placed within the bounds of their parent views and the text fields, labels, and buttons became clickable. I'm attaching the constraints that I used to fix the problem. Note: the constraints that are not expanded only include a height constraint.

How do I change text depending on where I am in my scrollview?

I have a makeshift custom "actionbar" at the top of my view, and a scrollView beneath it.
I was wondering if there is anyway to change the text displayed in the bar at the top depending on what is currently visible at my current position in my scrollView.
ANSWER
Make sure you have made your ViewController a ScrollView delegate, it should look like this:
#interface ViewController : UIViewController <UIScrollViewDelegate>
Once you have declared your ScrollView inside your .h file and synthesised it in the .m file make sure you tell it that it's delegate is itself:
ScrollView.delegate = self;
Then use the following method to detect where you are in your current scrollview
- (void) scrollViewDidScroll:(UIScrollView *)scroll {
if(scroll.contentOffset.y > /*Your Y coordinate*/){
// What you want to do when it reaches your y coordinate.
}
Implement UIScrollViewDelegate, and use the scrollViewDidScroll function. This will notify you every time the scrollView is scrolled. You can get the current location of the scrollView, and update your text accordingly.
More help on Apple Documentation

iOS - Programatically add a new view to work with my storyboard autolayout viewcontroller

I am using Storyboards with autolayout where I have a viewcontroller full of views, buttons, labels, a table...
I want to create a new View at the top of this view, below the navigationbar, when a button is clicked, and hide it after clicking again over it.
I want to do this programatically. This means that this view will appear at the top and the rest of the views will have to move down the height of that new view. When this view disappears the views will move up again.
What'd be the best approach to do this? I've tried to create a view in the storyboard with height = 0 and change the height in code. Is there a better way for this?
Hi as you are using auto layout I would suggest taking an IBOutlet for the height constraint of the view that you want to put below the navigation bar and change it in code
Here is a sample
//IBOutlet for height constraint of view
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *conHeightTopView;
- (void)changeHeight:(BOOL)change{
if (change) {
_conHeightTopView.constant = 50;//Height of view when shown
}else{
_conHeightTopView.constant = 0;
}
}
All the views that need to be moved down should be put in one UIView. And your code should have an IBOutlet for this view.
When you create your new view programmatically, you will know it's height.
Now you can call setFrame of your first view and move it down.

UIScrollView not working with Autolayout called from a Tab Bar Controller

I have created a very simple test environment one Tab Bar Controller and one View Controller with the following structure:
UI TAB BAR Controller ---------> UIScrollView
UIScrollView
View
Scroll View
Label
Label
Label
.h file
#interface rpViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#end
.m file
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.scrollView.contentSize = CGSizeMake(320, 1000);
}
If the start entry point for the app is the view with the scroller everything works fine!
If the start entry point is theTab Bar Controller the scroller is not working?
If your going to work purely with Autolayout you should not be doing any direct manipulations of frames, bounds or contentSize. Apple has written a technical note about working with UIScrollView and Autolayout Technical Note TN2154 that you should read.
So to answer the question, if you are going to be using auto layout you cannot manipulate contentSize and expect consistent results.

UITextField as child of it's own inputAccessoryView

I'm attempting to duplicate a UI similar to Messages. I have a UIToolbar pinned to the bottom of the view in a xib. On that toolbar is a UITextField. I've set the UITextField's input AccessoryView to be the UIToolbar. When I click on the UITextField, it snaps into position, but there's no keyboard underneath, just blank as if the keyboard was hidden. When I click on the UITextField a second time it animates with the keyboard. I've attempted to become it's delegate and all the other suggestions in other SO posts, but no luck.
#implementation ChatViewController {
IBOutlet UIToolbar *_keyboardAccessory;
IBOutlet UITextField *_textField;
}
- (void)viewDidLoad {
[super viewDidLoad];
[_textField setInputAccessoryView:_keyboardAccessory];
}
It will be simpler if you don't try to make the toolbar be the text field's inputAccessoryView. Just animate the toolbar up with the keyboard appears, and down when it disappears.
Everything you need to know to do this animation is documented in “Managing the Keyboard” in the Text, Web, and Editing Programming Guide for iOS. You can also find answers on stack overflow describing the process. here is an answer describing the process step-by-step with all the code.

Resources