I'm new to swift and I'm trying to move a UITextField up when the soft keyboard opens.
When I call view.frame.origin.y = -150 from the UIViewController, the UIView is moved but the UITextField inside it is not. (The UITextField "jumps" and animates back to its original position.)
It was working some time ago and I'm not sure what happened.
Found the problem.
I had a constraint mytextfield.Top = Top Layout Guide.Bottom, when it should be mytextfield.Top = SuperView.Bottom
I think LucaD is correct - you're missing constraints between your UITextField and UIViewController.
If you are using Interface Builder, see Apple's Adding Layout Constraints by Control-Dragging
Otherwise, if you are creating the UITextField programmatically, see SWIFT | Adding constraints programmatically
Related
I'm having a bit of trouble with the right parts of the view hierarchy in my iPhone app getting the touch events they should be. The code is all Swift in iOS 11.
I have a UIControl subclass which overrides beginTracking() and endTracking(). Originally, I had programmatically created a UIView, and then added an instance of my UIControl subclass as a subview. The code basically boiled down to:
myView = UIView()
view.addSubview(myView)
// add autolayout constraints for the view
myControl = TheControl(frame: theFrame)
myView.addSubview(myControl)
// add autolayout constraints for the control
This worked fine, but I wanted to have myView in Interface Builder to make autolayout a bit easier. So I got rid of the 'myView=UIView()' and 'view.addSubview(myView)' lines, created the view in IB, and connected it to an outlet in the view controller. The rest of the code stayed the same. So I programmatically create myControl, and add it as a subview of myView. Now that I have done this, myControl no longer receives touch events.
I can't figure out what is happening differently by doing this in Interface Builder vs. in code. I have toggled on and off "User Interaction Enabled" on myView in IB, and as far as I can tell it doesn't make any difference.
What am I doing wrong here?
As below image shows, in the storyboard, I put a UIScrollView (say called A) under navigation bar and another UIScrollView (say called B) inside A. B is at the top of A.
However, in the simulator, there is big margin between B and the up bound of A (I didn't use any autolayout or write any code). Can anyone tell me the reason? Thanks.
add the below line in your - (void)viewDidLoad method
self.automaticallyAdjustsScrollViewInsets = NO;
hope it will fix your issue.
Even though you're not using Auto-layout, you have Auto-layout checked on in your Storyboard. Because you do, UIScrollViews and any Subclasses of it (UITableView, UICollectionView, etc.) all automatically adjust for the UINavigationBar height when in a UINavigationController. This is implicit and there's no way to turn it off. The only solution is to "offset the offset" or to move the UIScrollView away from the UINavigationBar.
The big margin size is navigation bar height, try add a top autolayout for your scrollView.
I have 2 UITableView in my UIViewController. one of them is in the right side of the screen till center of screen. The other is from center till left bound.
User can hide the right UITableview by pressing one a button on the navigation bar.
I use this code to make left UITableview full screen:
self.rightTableView.hidden= YES;
self.leftTable.frame= CGRectMake(0,
self.dataTableView.frame.origin.y,
self.view.frame.size.width,
self.dataTableView.frame.size.height);
But it is not working.
I even put this code on the viewdidload and viewDidAppear methods but the frame not changing.
Try self.leftTable.translatesAutoresizingMaskIntoConstraints = YES, then call to frame resize at runtime is automatically translated into new constraints.
But, translatesAutoresizingMaskIntoConstraints will also cause new constraints to be added based on the view's autoresizingMask. So make sure other constraints are working properly.
When using autolayout, modifying the frame will have no effect.
Instead, you should modify the constraints.
You can do this by creating outlets for your constraints and connecting then in the interface builder.
In Interface Builder :
I am not using Autolayout.
I place my UIScrollView and then drag in other elements into the UIScrollView
When I run my app in the simulator, the subviews of the UIScrollView are positioned in the center of the UIView instead of where I placed them at.
One item is a UITextField and if I dismiss the keyboard, the view repositions itself to how I set it up in Interface Builder.
How can I fix the initial issue of all items being placed in the center of the UIView?
Edit:
If I NSLog the x and y of my UITextField, it shows the correct values of where I set it in Interface Builder, but when the view first appears, the text field is pushed down below that location.
In this case, the x is 20 and the y is 108
Solution:
I did have to check on the autosizing, but I also added the code below and it fixed my issue:
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
I am not sure whether this may help you..
Please try to check the Autosizing of every component in the uiscrollview. The issue may also occur because of the wrong Autosizing of components.
I face the similar issue and got fixed once I correct the Autosizing of every component.
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.