How to change constraint of textfield programmatically? - ios

i have menu icon and text field. When textFieldDidBeginEditing method called, i want to hide menu icon and change textfield's position to safe area top. I have connected textfield's top with menu icon. So i need to add constant with safe area programmatically.
Thanks.

Take the outlet of heightConstraint of menu icon and set it zero. On textview endediting set it on its default position.
How it Works:
Before textView began editing
textView.top(w.r.t safearea) = textView.top(w.r.t menu button) + btnmenu.height + btnMenu.top (w.r.t safearea)
After textView began editing as btnmenu.height = 0
textView.top(w.r.t safearea) = textView.top(w.r.t menu button) + btnMenu.top (w.r.t safearea)
Diametric Explanation:
Left : Before textView began editing
Right : After textView began editing as btnmenu.height = 0
Note : You can take outlet of constraints of BtnMenu.top & BtnMenu.bottom and set them as per your need.
Hope now you will be cleared.

Related

Set UiTextField cursor on top Swift IOS

I have a TextField in my storyboard with a height of 100 .
When I'm clicking the TextField the cursor is placed in center :
I would like to set the cursor position in the top left corner.
I tried :
let startPosition: UITextPosition = textField.beginningOfDocument
But it seems that the position is already at the beginning and when the height is extended the cursor stay in center.
How can it be done ?
Seems like you want a multiline UITextField. In that case you might want to take a look at UITextView, which should allow for what you want (docs)

how to remove a button for a particular cell in tableview in swift 3

I am having a tableview in which radio button edit and delete buttons are placed in a particular table view in which the key value pair default_shipping is true then the delete button should be hided and edit button needs to move that place and here the user is able to change the default address by clicking edit button and can make another address to be default so that the selected address should have only edit button and the edit button should be placed in position of delete button can anyone help me how to implement this ?
Give Edit button's Trailing constraint to contentView instead of
delete button.
Take outlet of Edit button's Trailing constraint in UITableViewCell's class
Use below code in cellForRowAtIndexPath:
if indexPath.row == "your default_shipping cell's indexPath.row" {
editButtonTrailing.constant = 8
} else {
editButtonTrailing.constant = space between delete and edit button.
}

How to modify view controller after pressing a button?

Let's say that if you press the button from the bottom of the screen after typing something in the account text field, you would be required to also enter the password like in the second image.
How should I do this? I don't think that creating a new view controller would be good. So, should I somehow modify the same view controller?
How could I add the new password text field under the account text field?
Keep in mind that they are still centered. Hiding and unhiding wouldn't work in this case and I also need to modify more things than only adding that text field.
First, create a UIView with everything you need on them. in this example I will have only two text fields and they are all color coded.
The view needs to be centered both horizontally and vertically, with width and height. Set identifiler for the height constraint to be updated later. Set the clip to board to true so that when we redeuce the height of the view, text fields below will hide. The settings for the view will be like this
For your text fields, they must have a constant padding to top. In my example, they are set to be in center horizontally, having a constant height, width and padding to opp.
Now, all you need to do is to get the height of the view from code and set the height to show or hide the text fields.
var flag = true
#IBAction func click(_ sender: Any) {
if flag {
flag = false
let filteredConstraints = theView.constraints.filter { $0.identifier == "viewHeight" }
if let heightConstraint = filteredConstraints.first {
heightConstraint.constant = 60
}
} else {
flag = true
let filteredConstraints = theView.constraints.filter { $0.identifier == "viewHeight" }
if let heightConstraint = filteredConstraints.first {
heightConstraint.constant = 128
}
}
}
Here is the code running in simulater.
another option is you can make tableView at Center, you need to create tableview height constraint outlet,
then you can maintain a counter how many time you want to add View, the counter should be return in tableView numberOfRowsInSection,
and you can make this view in Prototype Cell or using NIB, then just adjust header label text, textField placeholder and text on specific cell index,
when you increase or decrease counter update tableView Constraint, example you have a cell height as 50, in first case when you have on cell in tableView, you can set your tableView height constraint's constant as 50, when cells are two the 100 and so on.....
I think it's a simple logic

How to limit iOS button title with 0 lines in background area?

As above picture, the blue area is the button, but the title with 0 lines is out of the area, also I tried that set the titleLabel frame with the frame with the button, but still it does not work, so any tips here??
Clip the content to bounds:
myButton.clipsToBounds = true
may be this will help:
1) add a button
2) set line break (see screen)
3) set multimple lines in text (use alt+enter to new line )
4) Set constraints for leading and for top - so width and height can be calculated by autolayout
5) set background color if you need

Updating constraints programmatically

Situation is like I have a view with four buttons
There comes a condition when button with text Search Community should be hidden and width of Options button should get increased
Using Constraints for the first time in my project, I am not getting how to achieve this. Set of constraints added on both these buttons would be clear from the following two images.
The problem I see with your setup is that you have this constraint between the 2 buttons of equal width. This is true in the first case but when you want to hide one of them, it is not anymore.
So, you will need to rethink your constraints a bit. Maybe instead of the equal width constraint, you could use a static width constraint one the first button(the left one, that you want to hide). Then the second one, will just have a horizontal space constraint to the first one, and a trailing space to the superView.
Then you make an outlet in the VC for the width constraint of the first button and when you want to hide it you do something like this:
self.searchButtonWidthConstraint.constant = 0
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.view.layoutIfNeeded() // if you want to animate the constraint change
})
Let me know if you have questions. Good luck and hope it works out!
Update
For the landscape use case, you could listen to the orientation change notification, to update the width constraint of the button
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
self.searchButtonWidthConstraint.constant = LANDSCAPE_WIDTH
UIView.animateWithDuration(duration, animations: { () -> Void in
self.view.layoutIfNeeded() // if you want to animate the constraint change
})
}
Add constraint to 1st Button
Leading Space To NewView(gray colored)
Bottom space to NewView
Top Space to Topbutton (blue colored in your 1st and 2nd image)
Width (from Bottom - Right portion )
Add Constraint to 2nd Button
Trailing Space to NEwView
Bottom space to NewView
Top Space to Topbutton (blue colored in your 1st and 2nd image)
Horizontal space FirstButton ( Search Community )
Width

Resources