Using auto layout and creating dynamic interface (realigning automatically when field hidden) - ios

Using Xcode5 and auto layout. Consider following scenario:
I have an outlet for "Dispatch" UITextField
#property (weak, nonatomic) IBOutlet UITextField *dispatchTextField;
If user clicks "Camera" button I want to hide dispatchTextField and move "Subject" and "Body"(below) up.
This is not a real scenario but I'm facing tasks where I will be using this kind of technique. I've seen code samples where container size can be modified and so on. In XAML - there is "StackPanel", in Android there is similar controls where I can just hide this TextField and views below automatically spring up.
So, what is the proper way to do this in XCode5 with auto-layout?
I tried (with no luck)
self.recipientTextField.hidden = YES;
I also tried
[self.recipientTextField setFrame:CGRectMake(0, 0, 0, 0)];
[self.view layoutIfNeeded];

Add a height constraint to the text field. After making sure there are no conflicting vertical constraints, add a reference to this constraint in your view controller.
Add an action to the Camera button and connect it to the view controller. Whenever the button is tapped, you would set the constant property of the height constraint to zero.
Make sure that other views are setup to have constant vertical spacing with the text field you wish to shrink.

Related

add inset to textfield created in storyboard?

is there a way to add inset in an existing UITextField which was created in the storyboard? I added the constraints, an IBOutlet and I use :
fromTF.textRectForBounds(CGRectInset(fromTF.bounds, 50, 0))
But there is not the inset. Is it only possible to add inset if the textfield is created dynamically?
There might be a way to accomplish what you're trying to do by subclassing the textField. (this might help if you go this route: Text inset for UITextField?)
Alternatively, when I run into this problem, I do a few things in storyboard to give the appearance of text inset:
1.) Set the styling in the attributes inspector for the UITextField so that it doesn't have a border or anything - should just be a clear box that you can tap and enter text in.
2.) Then create a container UIView behind it to the full width that you want your text field area to be.
3.) Decrease the width of your UItextfield and align it to the right edge of your container view so that you have the appropriate amount of space on the left.
4.) Then just add some constraints for your container and your UItextfield.
textRectForBounds: is a function, not a property, you can't "set" it like you are trying to do. Check the documentation for UITextField.
You should not call this method directly. If you want to customize the
drawing rectangle for the text, you can override this method and
return a different rectangle.
If you want the appearance of an inset solely through interface builder, do as Adama has said and place a UIView behind the text field, make the textfield background clear, and set the UIView's background color such that it looks like it is the background of the textfield.

How to move scrollview upward in autolayout

In Signup page , I have many text fields in UIScrollVoew , so like other app i want to move Scroll upward once textfields at bottom are tapped , but i am not sure that old way which were using will work or not ? can any body please help what is best way of moving scroll automatically using autolayout
Use the custom class to autoScrolling click on last Textfield . Check it
You can define outlets in your code and set their sizes for your need. Something like this.
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewBottomConst;
self.textViewBottomConst.constant = height;

UIAlertView causes UIButton whose position is changed at runtime to go back to original position

So I have a storyboard with autolayout on (I need it for other views, not this one, but i cannot switch off autolayout for just one view). I change two UI elements' positions in viewDidLayoutSubviews.
Then I tap one of the UI elements, which shows an UIAlertView, which causes this UI element to go back to its original position (specified in storyboard). However (!) the other element stays in place. I tried to implement UIAlertViewDelegate to put button back(in
– willPresentAlertView:
– didPresentAlertView:
– alertView:willDismissWithButtonIndex:
– alertView:didDismissWithButtonIndex:
),
but it didn't work (works in didPresentAlertView, but no in any dismiss ones).
Is there a way to keep the button in place and to keep the autolayout on.
You need to change the constraint rather than the frame for the view you are moving. Add an Outlet to the constrain you want to change (x or y position), and you will need to update the constraint in your viewDidLayoutSubviews method.
When the UIAlertView is displayed, an animation occurs, which resets the frames in your view. Since you didn't change the constraint in the first place (just the view's frame), it gets brought back to the original position.
If you have a constraint:
// .h file
/// Hooked up to your Storyboard file
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *myConstraint;
// .m file
self.myConstraint.constant = 300; //Your new position in either x or y.

Can't Wire to Subview in IB

Quick question. Using IB, I have a subview in a ViewController. In that subview I have a label, which I would like to wire to my custom subview class. However, IB will not let me. What am I missing?
I also tried to add the label programmatically; however, it appears that the frame was not ever set. I could hard code the size of the label, but I could not make it dependent on the frame size of my subview, because the frame and the bounds were always zero rects, even after the view showed up in my view controller at a non zero size. Any ideas here would also be much appreciated.
You are actually completely right. It wont let you connect from IB to the Header of a custom view in Xcode 4.6.2
Personally I would file a Radar but I would want to do a bit more research to prove it and as this is a pattern I wouldn't ever use then I won't.
Fortunately you can get around it
Make sure your custom view is configured correctly in IB
and assuming you are setup something like this
Then you can manually declare in your header
#interface MyCustomView : UIView
#property (weak) IBOutlet UILabel *label;
#end
And drag FROM the dot that appears beside the property TO the label.
Or drag FROM the right-click HUD of the custom view TO the label.
Neither case will work by dragging from the label to the view.
In your header file, you need to define the label as an IBOutlet, then you can drag from your file's owner to the label.
IBOutlet * lblSomeLabel;
Disable AutoLayOut and try again.

Repositioning textview with Storyboard

I'm struggling with a strange issue... I've set everything up using Storyboard.
Depending on the size of text contents an uitextview has, I have to programmatically resize & reposition it. However, it seems like it's not repositioning as I wish :(
-(void)viewWillAppear:(BOOL)animated
{
//ansContent is connected to the uitextview via IBOutlet
ansContent.frame = CGRectMake(27, 90, 270, 10000);
ansContent.text = #"very very long text here";
}
It seems like the changing the text works... not the frame :S What is wrong with it?
All I did in Storyboard was to drag & drop an uitextview and connect it to ansContent.
Disable the auto layout option in storyboard. In your case, the autolayout is preventing it from displaying it in proper location. Once you have disabled it, you can set the frame programmatically.

Resources