How to change UINavigationBar height? - ios

Is it possible to change the UINavigationBar height in ios 11?
I've tried this:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let height: CGFloat = 50 //whatever height you want to add to the existing height
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
}
and this:
extension UINavigationBar {
override open func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 100)
}
}
and none of them seem to work.

Related

TabBar select item change background color

I try change background color, when user tap on tab bar item. My code:
//MARK: func
func settingTabBar(){
//настроили бэкгроунд активного таб бара на зеленный цвет
let numberOfItems = CGFloat((tabBarController?.tabBar.items!.count)!)
let tabBarItemSize = CGSize(width: (tabBarController?.tabBar.frame.width)! / numberOfItems, height: (tabBarController?.tabBar.frame.height)!)
tabBarController?.tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: Helpers.followGreenColor, size: tabBarItemSize).resizableImage(withCapInsets: .zero)
// remove default border
tabBarController?.tabBar.frame.size.width = self.view.frame.width + 4
tabBarController?.tabBar.frame.origin.x = -2
}
Select item color changes but not completely. Look screen below. THX.
upd:
I solved the problem with the following code
class ChildTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
changeBckgColorSelection()
}
fileprivate func changeBckgColorSelection() {
let imgSize = CGSize(width: tabBar.frame.size.width / CGFloat(tabBar.items!.count),
height: tabBar.frame.size.height)
UIGraphicsBeginImageContextWithOptions(imgSize, false, 0)
let p = UIBezierPath(rect: CGRect(x: 0, y: 0, width: imgSize.width,
height: imgSize.height))
Helpers.followGreenColor.setFill()
p.fill()
let finalImg = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.tabBar.selectionIndicatorImage = finalImg
}
}

UIWebView sizeThatFits only works in delegate method

So I use a UIWebView like a UILabel with a UITextBox that changes its text via
#IBAction func textChanged(_ sender: Any){}
I size the UIWebView to the size of its content with a fixed width and a flexible height using
override func viewDidLoad() {
super.viewDidLoad()
label = WebLabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 1))
label.delegate = self
label.layer.borderColor = UIColor.black.cgColor
label.layer.borderWidth = 1.0
self.view.addSubview(label)
}
Where WebLabel is a class derived from UIWebView which implements following method :
func set(query: String)
{
let htmlString = makeHTMLString(input: (query))
self.loadHTMLString(htmlString, baseURL: nil)
self.sizeToFit()
}
So with this textChanged I get my desired result :
#IBAction func textChanged(_ sender: Any) {
label.set(query: (sender as! UITextField).text!)
label.frame = CGRect(x: label.frame.origin.x, y: label.frame.origin.y, width: label.frame.width, height: 1)
let size = label.sizeThatFits(CGSize.zero)
label.frame = CGRect(x: label.frame.origin.x, y: label.frame.origin.y, width: label.frame.width, height: size.height)
}
But I also want to set a initial html text. I do this using:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
label.set(query: "Hello World !")
label.frame = CGRect(x: label.frame.origin.x, y: label.frame.origin.y, width: label.frame.width, height: 1)
let size = label.sizeThatFits(CGSize.zero)
label.frame = CGRect(x: label.frame.origin.x, y: label.frame.origin.y, width: label.frame.width, height: size.height)
}
But that doesn't work. So the code seems to only work when triggered by user in the delegate method. How do I solve this problem ? Thanks a lot for your help !

How can I change height of Navigation Bar - Swift 3

I'm pretty sure this is not duplicate because I looked at other answers and they all outdated and do not provide exact answer.
I have Navigation Controller and several view controllers. I want to make Navigation Bar a bit taller so it would fit text size that I need. How can I do that ?
I tried this:
UINavigationBar.appearance().frame = CGRect(x: 0.0, y: 0.0, width: 320.0, height: 210.0)
but nothing happens...
Also, I wasn't able to add any constraints to the Nav Bar using Xcode layout buttons.
Hope somebody can help me to fix this issue. Thanks in advance!
UPDATE 8/3/20: I posted this in 2016. A number of people have stated this no longer works so please use at your own risk. I am not working in iOS at the moment so I do not have an update handy. Best of luck!
Here is one way to do it:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let height: CGFloat = 50 //whatever height you want to add to the existing height
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
}
You can write this code in class that extends UINavigationController
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let height = CGFloat(72)
navigationBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: height)
}
You can create a class based on UINavigationBar like this
class TTNavigationBar: UINavigationBar {
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 55)
}
}
And add it to your NavigationController.
This might be better if your navigation bars in your app all have same adjusted height.
extension UINavigationBar {
open override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 51)
}
}
you can draw your navigation bar from xib (with your height) and then:
self.navigationController?.navigationBar.addSubview(yourNavBarView)
in alternative, if you want create UIView from code,
let viewNavBar = UIView(frame: CGRect(
origin: CGPoint(x: 0, y:0),
size: CGSize(width: self.view.frame.size.width, height: 100)))
and then adding to nav bar.

Swift - Parallax Header View - ScrollView overlap cells

I am trying to custom a extension for Parallax Header. However, it's not working perfectly. The table header view always floats and overlaps cells.
Extension code:
extension UITableView {
func addImageHeaderView(headerView headerView: UIView, height: CGFloat) {
self.contentInset = UIEdgeInsetsMake(height, 0, 0, 0)
self.contentOffset = CGPoint(x: 0, y: -height)
self.tableHeaderView = headerView
self.tableHeaderView?.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: height)
}
func updateHeaderView(height kTableHeaderHeight: CGFloat) {
var headerRect = CGRect(x: 0, y: -kTableHeaderHeight , width: self.bounds.width, height: kTableHeaderHeight)
if self.contentOffset.y < -kTableHeaderHeight {
headerRect.origin.y = self.contentOffset.y
headerRect.size.height = -self.contentOffset.y
}
self.tableHeaderView?.frame = headerRect
}
}
Implementing Code :
tableView.addImageHeaderView(headerView: viewHeader, height: 100)
func scrollViewDidScroll(scrollView: UIScrollView) {
tableView.updateHeaderView(height: 200)
}
Am I wrong at something? Please show me if you know.
Can you please try to set the following in viewDidLoad
self.edgesForExtendedLayout = UIEdgeInsetsZero;
in swift
self.edgesForExtendedLayout = .None
Also what you can try to do is to check the result of the following
tableView.addImageHeaderView(headerView: viewHeader, height: 0)
func scrollViewDidScroll(scrollView: UIScrollView) {
tableView.updateHeaderView(height: 200)
}
please notice that i changed the height from 100 to 0
i did it because the height value will change the contentInset of your table view and this is exactly the distance from the top corner

set the padding of a text field using swift in xcode

How to create some space at the beginning of a text field using swift?
I looked at the post
padding of text field
I don't understand what the subclass is doing and how do I use that class to padding.
Thank you very much
https://stackoverflow.com/a/27066764/846780
This answer is very clear,
If you subclass UITextField you can override textRectForBounds, editingRectForBounds and placeholderRectForBounds. These methods just allow you to add a rect (frame) to your textfield's label.
newBounds method create the rect (frame) that will be added to textfield's label.
Finally your padding is: let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5);
Now you have a custom UITextField that has a custom padding.
If you create your new subClass like this class MyTextField: UITextField for example, you only need to change the class of the UITextField that you've added into IB file.
Complementing the answer of klevison-matias this is the code I use when I want to add a padding to my TextField in Swift 3
//UITextField : override textRect, editingRect
class LeftPaddedTextField: UITextField {
override func textRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y, width: bounds.width, height: bounds.height)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y, width: bounds.width, height: bounds.height)
}
}
Then in my TextField I use in this way:
let emailTextField: LeftPaddedTextField = {
let textField = LeftPaddedTextField()
textField.placeholder = "Enter email"
textField.layer.borderColor = UIColor.lightGray.cgColor
textField.layer.borderWidth = 1
textField.keyboardType = .emailAddress
return textField
}()
let passwordTextField: LeftPaddedTextField = {
let textField = LeftPaddedTextField()
textField.placeholder = "Enter password"
textField.layer.borderColor = UIColor.lightGray.cgColor
textField.layer.borderWidth = 1
textField.isSecureTextEntry = true
return textField
}()
Create TextField outlet and use following code. Say "nameTextField" has to add padding.
let paddingView = UIView(frame: CGRectMake(x: 0, y: 0, width: 30, height: 30))
nameTextField.leftView = paddingView
nameTextField.leftViewMode = .always
nameTextField.placeholder = "Enter Name"
You can add Image in paddingView.
Based on #Jorge Casariego's answer I came up with this solution.
You can set PaddedTextField class and the desired padding through the Interface Builder!
class PaddedTextField: UITextField {
#IBInspectable var padding: CGFloat = 0
override func textRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + padding, y: bounds.origin.y, width: bounds.width - padding * 2, height: bounds.height)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + padding, y: bounds.origin.y, width: bounds.width - padding * 2, height: bounds.height)
}
}
full code sample:
make sure your TextField points to RoundedTextField via Story board
import UIKit
class RoundedTextField: UITextField {
override func awakeFromNib() {
// add rounded corner
self.layer.cornerRadius = 15.0
self.clipsToBounds = false
super.awakeFromNib()
}
// For the padding from the left
override func textRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + 15.0, y: bounds.origin.y, width: bounds.width, height: bounds.height)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.origin.x + 15.0, y: bounds.origin.y, width: bounds.width, height: bounds.height)
}
}

Resources