How do you create padding for text field (UITextField) in swift? I want it to be global scope so when the UIView load every Text field have this padding apply to it.
Current Swift Code:
class MyCustomTextField: UITextField {
// Custom Code that i can't figure out.
}
Steps i try to take:
Apple developer document, it doesn't talk about inset or padding
override
Search Stackoverflow and see they either relate to objective-c or the
hack is removed when Xcode become 6.1
Google, nothing came up about Swift padding (only objective-c)
Thank you in Advanced.
Swift Beginner :)
class MyCustomTextField : UITextField {
var leftMargin : CGFloat = 10.0
override func textRectForBounds(bounds: CGRect) -> CGRect {
var newBounds = bounds
newBounds.origin.x += leftMargin
return newBounds
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
var newBounds = bounds
newBounds.origin.x += leftMargin
return newBounds
}
}
Related
Is it posible to change the placeholder text alignment in a MDCTextField that is using MDCTextInputControllerUnderline?
What I am looking for is despite the placeholder appears like this:
Place holder
.............................
I want it at the middle
Place holder
.............................
Thank you in advance!
yes you can if you can override this method of textfield
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
var rect = super.placeholderRect(forBounds: bounds)
rect.origin.x = rect.origin.x + CGFloat(paddingyouwant)
return rect
}
I have found answer to this question in below link.But is in objective C.Please help to get it's Swift representation.
Changing the location of the minus sign in UITableView in Edit mode
Working example in Swift 3.0
override func layoutSubviews() {
super.layoutSubviews()
for subview in self.subviews {
if subview.isMember(of: NSClassFromString("UITableViewCellEditControl")!) {
var newFrame: CGRect = subview.frame
newFrame.origin.x = 280
subview.frame = newFrame
}
}
}
Edit: Result
I want to implement something similar to Instagram activity screen header:
I added two buttons for Following and You which are insides an UIView(header). For the bottom blue bar I added an UIView.
Where exactly should I animate that UIView bar to move from Following to You screen when switching pages inside UIPageViewController?
Or maybe there is a better solution to do this ?
For something like this, I like using property observers to keep the API nice and clean for calling code. In the following example I am doing just that and manually adjusting the frame values, but in practice you'll probably want to use Auto Layout and animate your constraints.
class MyHeader: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
#IBOutlet weak var selectionView: UIView!
var buttons = [UIButton]()
var selectedTabIndex: Int {
didSet {
UIView.animateWithDuration( 0.5) {
let targetButton = self.buttons[ self.selectedTabIndex ]
var frame = self.selectionView.frame
frame.origin.x = targetButton.frame.minX
frame.origin.y = targetButton.frame.maxY - frame.height
frame.size.width = targetButton.frame.minX
self.selectionView.frame = frame
}
}
}
}
I have created an image for textfield in my iOS app. But the text seems to overlap the image. Is there any way to prevent this or achieve the same design in another way. Below is the image of the problem I'm facing.
Is there any way to make the text start from a particular position in the text field?
One solution is to make your UITextField a subclass of UITextField. This will allow you to override textRectForBounds: - and now you are in charge of where the text will go.
In particular, in your override, call super to get the original rect. Now increase the x-component of the origin by some amount to allow room for your image, and decrease the width of the size by that same amount. Return the resulting modified rect and you're done.
This will allow you to change the rect where the text will go:
class MyCustomTextField : UITextField {
var leftMargin : CGFloat = 10.0
override func textRectForBounds(bounds: CGRect) -> CGRect {
var newBounds = bounds
newBounds.origin.x += leftMargin
return newBounds
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
var newBounds = bounds
newBounds.origin.x += leftMargin
return newBounds
}
}
I did also find another way to achieve this by adding offset to the textfield. But found out this would not be an ideal solution when you have multiple text fields in the same view. Just adding the solution as a reference to others so that someone finds it helpful.
let paddingView = UIView(frame: CGRectMake(0, 0, 60, self.yourTextField.frame.height))
yourTextField.leftView = paddingView
yourTextField.leftViewMode = UITextFieldViewMode.Always
I am trying to change the height of my UIToolbar in a new iOS 7 project but I am not able to.
I am using a UINavigationController to manage a couple of UIViewController.
I tried setting the frame for the toolbar via the navigation controller but alas, the toolbar property is read-only.
I looked at "Is there a way to change the height of a UIToolbar?" but that did not work.
I tried subclassing UIToolbar, forcing a custom height and setting the right class in the Storyboard but that did not work neither, height keeps on being 44px.
I thought about auto-layout could not set any constraint on the size of the toolbar, every field is disabled.
I can set a custom view in a UIBarButtonItem with a bigger height than the toolbar. The big item will be correctly rendered but it will overflow from the toolbar.
This is the best I could do: screenshot
Is it actually possible to change the height of the UIToolbar in iOS 7?
Or am I supposed to create a bunch of custom items to mimic it?
Following the #Antoine suggestion using sizeThatFits, here is my Toolbar subclass with an height of 64:
import UIKit
class Toolbar: UIToolbar {
override func layoutSubviews() {
super.layoutSubviews()
frame.size.height = 64
}
override func sizeThatFits(size: CGSize) -> CGSize {
var size = super.sizeThatFits(size)
size.height = 64
return size
}
}
Then, when initializing the navigation controller, I say it should use that class:
let navigationController = UINavigationController(navigationBarClass: nil, toolbarClass: Toolbar.self)
The easiest way I found to set the toolbar height was to use a height constraint as follows:
let toolbarCustomHeight: CGFloat = 64
toolbar.heightAnchor.constraintEqualToConstant(toolbarCustomHeight).active = true
I've fixed this by subclassing UIToolbar and pasting the following code:
override func layoutSubviews() {
super.layoutSubviews()
var frame = self.bounds
frame.size.height = 52
self.frame = frame
}
override func sizeThatFits(size: CGSize) -> CGSize {
var size = super.sizeThatFits(size)
size.height = 52
return size
}
If you are using same height for all screens, this should do the trick
extension UIToolbar {
open override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 60)
}
}
Although many solutions point in the right direction, they have either some layout issues or doesn't work properly. So, here's my solution:
Swift 3, custom UIToolbar subclass
class Toolbar: UIToolbar {
let height: CGFloat = 64
override func layoutSubviews() {
super.layoutSubviews()
var newBounds = self.bounds
newBounds.size.height = height
self.bounds = newBounds
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
var size = super.sizeThatFits(size)
size.height = height
return size
}
}
You can customize the height of your UIToolbar in iOS 7 with the following code. I have it tested and working in my current project.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Make the Toolbar visible with this line OR check the "Shows Toolbar" option of your Navigation Controller in the Storyboard
[self.navigationController setToolbarHidden:NO];
CGFloat customToolbarHeight = 60;
[self.navigationController.toolbar setFrame:CGRectMake(0, self.view.frame.size.height - customToolbarHeight, self.view.frame.size.width, customToolbarHeight)];
}