Showing UIView back in same frame after removeFromSuperView - ios

I am using an UIView as container for UIButton. When user opens keyboard, I am removing from superview and adding in to TextField AccessoryView for sticky button design.
[_buttonView removeFromSuperview];
[_usernameTextField setInputAccessoryView:_buttonView];
But after that I add view back the view show top of the screen. I Set its frame but, it is also not working.
_buttonView.frame = CGRectMake(0, 561, 320, 106);
[self.view addSubview:_buttonView];
So I know that, removeFromSubView method removes view from memory, so frame is not stored. If I don't removeFromSubView AccessoryView is not adding it because of parent child issue.

Related

How to prevent iOS from resizing your UIViewController's view after return from background

I have a UIViewController that displays a form with several text fields. In order to prevent the text fields from getting blocked by the keyboard, I resize the controller's view when the keyboard appears and disappears.
However, when the keyboard is up, the user presses the home button, and then returns to the app, the controller's view will be resized again to the size it was before the keyboard was up and the keyboard will still be showing.
What's causing my controller's view to be resized on return from background, and how can I prevent it?
Maybe you need to nest a UIView,for example
_backgroundView = [UIView new];
_backgroundView.backgroundColor = [UIColor whiteColor];
_backgroundView.frame = CGRectZero;
[self.view addSubview:_backgroundView];
[_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(self.view);
make.height.mas_equalTo(self.view.mas_height);
}];
then you need add your custom UIView to this backgroundView.
as you said,UIViewController's view will be resized after return from background. so you can nest a UIView of the same size as self.view,and add your custom UIView to this UIView.
In order to prevent the text fields from getting blocked by the keyboard, you can resize this backgroundView when the keyboard appears and disappears. and this time when you click the home button to enter the background or return from background,self.view won't be resized and backgroundView won't be resized too.
Although it is a bit of a hassle, this will solve your problem and will not affect the user experience anymore. And if you have a better solution, please let me know
It sounds like you are setting the frame and not using autolayout. When the view reappears viewDidLayoutSubviews gets called and your frame gets recalculated obliterating your previous change. You can either:
1) Move your frame to viewDidLayoutSubviews and change its size only if the keyboard is showing.
2) Use autolayout and simply pull up your bottom constraint .constant by an amount equal to your keyboard height.
In both cases you should call layoutIfNeeded to trigger autolayout/viewDidLayoutSubviews when the keyboard appears/disappears. This behavior is a good example of why you should not manipulate your frames outside of viewDidLayoutSubviews except for transitory animations.

InputAccessoryView covers the bottom bar

Any idea how to get an inputAccessoryView to anchor to the tab bar rather than the bottom of the screen?
I have created a UIViewController and overridden the following methods:
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(UIView *)inputAccessoryView {
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
self.keyboardInputAccessoryView =[[BRKeyboardInputBarView alloc] initWithFrame:frame leftButtonTitle:#"Left" andRightButtonTitle:#"Send"];
[self.keyboardInputAccessoryView setDelegate:self];
[self.keyboardInputAccessoryView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.keyboardInputAccessoryView removeFromSuperview];
return self.keyboardInputAccessoryView;
}
View controller with inputAccessoryView covering the tab bar
By the looks of it the view controller adds the view to the window rather than the current view controllers view, which would explain its positioning. However if I remove the line:
[self.keyboardInputAccessoryView removeFromSuperview];
I get a crash when I tap in the textview of my accessory view:
The view hierarchy is not prepared for the constraint:<NSLayoutConstraint:0x7fa0c2ca5f80 BRKeyboardInputBarView:0x7fa0c2d6fad0.bottom == UIInputSetContainerView:0x7fa0c295a2c0.bottom>
So I guess what I am asking is what is the correct way to add a keyboard accessory view so that it plays nicely with auto layout and avoids the crash, but also anchors itself to the view and not the window?
What you are seeing is the right behaviour.
The results you are seeing is because of the fact that UIViewController is a UIResponder subclass. By overriding the inputAccessoryView and returning an instance of a view, UIViewController will take care of placing that view at the bottom of the screen and animating it appropriately when keyboard appears or disappears.
If you want to add this bar on top of your keyboard, then you need to set the property inputAccessoryView of a textField/textView to your custom view.

iOS View gets reset on addSubview

I have a storyboard with a square imageView in the middle at: 280,140 (vertical)
When the application starts i am able to move this imageView by pressing buttons. I move the imageView using:
_mainImage.frame = CGRectMake(_mainImage.frame.origin.x + 2, _mainImage.frame.origin.y, _mainImage.frame.size.width, _mainImage.frame.size.height);
while the application is running i keep adding new imageViews to the mainView:
UIImageView *imgView;
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, yCoordinate, 50, 10)];
imgView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:imgView];
each time i call this code the square imageView i have placed in the middle and moved around, gets set back to its starting position. It seems like the mainView gets "reset" each time the addSubview gets called. Is there any way i can add a subview without having the mainView "reset"?
Most likely your view is set up using Autolayout (this is the default for storyboards or nibs) and when you add a new subview, a layout pass is performed which resets your views position back to that defined by its original constraints.
You should move your view by updating its constraints, or turn off Autolayout. There are lots of posts around explaining how to do either of these things.

iOS 7 UIScrollView doesn't scroll when presented as modal view controller, works fine otherwise

I have a storyboard in which I have a view controller, (InfoViewController) in which I have an UIScrollView with some labels, uitextviews, etc. this is all created in IB, no code has been written at all. The only thing that is left for me to do is to set the content size, which I do as following:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
CGRect screenRect = [[UIScreen mainScreen] bounds];
[self.scrollView setContentSize:CGSizeMake(screenRect.size.width, 600)];
[self.scrollView setBackgroundColor:[UIColor greenColor]];
}
Whenever I make this view the entry point of my app, it works perfectly. I can see my view, the content size is set, the background color is being set to green.
Now it comes, I created another view controller, and this view controller is now my entry point of the app. I added a button in there, and on this button I did a "modal segue" to the earlier mentioned Info View Controller.
When I now run my app, I press this button, my Info View Controller shows up. The green background color is being set, but it's impossible to scroll. So the code is being executed (otherwise the background color couldn't been green, in the storyboard it's just plain white) but somehow whenever I use this "modal segue", the scroll functionality gets lost.
How can I fix this?
Try to insert a UIView into the scroll view...
Set the UIView with top, bottom, leading and trailing space to super view to 0.
Then insert everything into the UIView rather than into the ScrollView
Then modify the constraint height of the inner UIView instead of the contentsize of the scroll view, it works with iOS7

only Scrolling programmatically?

I don't know if this makes sense at all. I have UIScrollView from interface builder hooked it up as an outlet on top of my UIScrollView I have a UIImage view which holds an image called Scroll Background it is an extra half in screen real estate - my app is landscape and the image is an about half the screen taller. The image is hooked up as an outlet too. I have a button on a toolbar that brings up the keyboard when the button is pressed I'd like to programmatically scroll to the bottom of the UIImage view and when it is resigned I'd like to programmatically scroll to the top. I don't want the user to be able to scroll just for the app to scroll programmatically.
I can't seem to get this method to work and I'm not sure why :/
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
scrollRectToVisible:animated: is a non-intuative method to use in my opinion. In order to accomplish what you are after you should be able to set userInteractionEnabled to NO on the UIScrollView that will prevent users from scrolling.
Then in order to scroll the view programmatically you can call scrollRectToVisible but you need to give it a CGRect that is representative of the area you want to show. So in your case to scroll to the top:
CGRect visibleFrame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds) , CGRectGetHeight(self.view.bounds));
[self.myScrollView scrollRectToVisible:visibleFrame animated:YES];
Hope this helps!

Resources