how can I remove the top border on UIToolBar - iphone

I have set my UIToolBar tint color to some value, and there is this border line that I see in which I want to remove:
How do I remove this black border>

You can do like this:
self.navigationController.toolbar.clipsToBounds = YES;

toolbar1.clipsToBounds = YES;
Worked for me incase someone is still trying with Navigational bar

Correct answer is the one by totalitarian...FYI. https://stackoverflow.com/a/14448645/627299
My response is still below for reference.
Here's what I did with my WHITE background toolbar...
whiteToolBar.layer.borderWidth = 1;
whiteToolBar.layer.borderColor = [[UIColor whiteColor] CGColor];
Perhaps you could do the same thing with your color instead.

setShadowImage to [UIImage new]

navigationController?.toolbar.barTintColor = .white
navigationController?.toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)

Setting the style to UIBarStyleBlackTranslucent did it for me (iOS 6)

create a 1 pixel x 1 pixel clear image and call it clearPixel.png
toolbar.setShadowImage(UIImage(named: "clearPixel.png"), forToolbarPosition: UIBarPosition.any)

this doesn't work consistently on iOS versions, doesn't seem to work on iOS7. i answered this in another question: https://stackoverflow.com/a/19893602/452082 and you can modify that solution to just remove the background shadow (and leave your toolbar.backgroundColor whatever color you like)

The clipsToBounds technique clips the UIToolBar's shadow as well as the background view. On an iPhone X, that means the background no longer reaches outside the safe area.
The solution below uses a mask to clip only the top of the UITabBar. The mask is rendered in a UIToolBar subclass, and the mask frame is kept updated in an override of layoutSubviews.
class Toolbar: UIToolbar {
fileprivate let maskLayer: CALayer = {
let layer = CALayer()
layer.backgroundColor = UIColor.black.cgColor
return layer
}()
override init(frame: CGRect) {
super.init(frame: frame)
initialize()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
fileprivate func initialize() {
layer.mask = maskLayer
// Customize toolbar here
}
override func layoutSubviews() {
super.layoutSubviews()
// height is an arbitrary number larger than the distance from the top of the UIToolbar to the bottom of the screen
maskLayer.frame = CGRect(x: -10, y: 0, width: frame.width + 20, height: 500)
}
}

I got a bit confused with these answers but I was missing the point that you were using an Outlet so just to be clear here is the swift code I used to hide the border:
import UIKit
class ViewController: UIViewController {
//MARK Outlets
#IBOutlet weak var ToolBar: UIToolbar!
//MARK View Functions
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Hide the bottom toolbar's top border
ToolBar.clipsToBounds = true
}
}
I had dragged a toolbar to the bottom of a view for this and it's not the top nav bar some other questions refer to.

[[UIToolbar appearance] setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setShadowImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionBottom];
[UIToolbar appearance].barTintColor = [UIColor ...];```

Related

Strange cornerRadius behaviour on UIView from xib

I created a custom Numpad keyboard through xib and wanted to initialize it with a rounded corners.
Here is the code I use:
import UIKit
class NumpadView: UIView {
#IBOutlet weak var resetButton: NumpadButton!
#IBOutlet weak var decimalButton: NumpadButton!
var target: UITextInput?
var view: UIView?
init(target: UITextInput, view: UIView) {
super.init(frame: .zero)
self.target = target
self.view = view
initializeSubview()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
initializeSubview()
}
func initializeSubview() {
let xibFileName = "NumpadView"
let view = Bundle.main.loadNibNamed(xibFileName, owner: self, options: nil)![0] as! UIView
self.layer.cornerRadius = 30
self.layer.masksToBounds = true
self.addSubview(view)
view.frame = self.bounds
self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
}
But then I receive the strange view look in area where cornerRadius is implemented:
How to remove that grey background which is visible near the rounded corners?
UPDATE:
According to View Debugger it seems like this grey layer between yellow square and Visual Effect View is a UICompatibilityInputViewController:
How I presenting the Numpad:
1.I created a NumpadView as a UIView subclass in a xib:
2.In my VC I just change a standard textField.inputView property on my custom NumpadView:
import UIKit
class NumpadViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
setupKeyboardHide()
textField.delegate = self
textField.inputView = NumpadView(target: textField, view: view)
}
}
Test project on Github
Another option to get your "rounded corners"...
Get rid of the Visual Effect View in your Numpad class, and set a layer mask on the superview at run-time.
In class NumpadView: UIView, UIInputViewAudioFeedback:
override func layoutSubviews() {
super.layoutSubviews()
guard let sv = superview else { return }
let maskLayer = CAShapeLayer()
let bez = UIBezierPath(roundedRect: bounds, cornerRadius: 16)
maskLayer.path = bez.cgPath
sv.layer.mask = maskLayer
}
Looks like this:
Sure, the NumpadView object has rounded corner. However, you are using it like this:
textField.inputView = NumpadView(target: textField, view: view)
So, that part which is not rounded is the textField's inputView. I'm not quite sure if you can modify its layer to have corner radius. But, if you really want to get that rounded corner effect, an easier approach is just to add the NumpadView directly to the parent view and anchored to the bottom. Then show it via the begin editing delegate of the textfield (and hide via end editing).

White Space at the bottom of a tableview

I have a viewcontroller with a tableview inside it. At the bottom of the tableview there is some white space that I want to get rid of and be replaced by my background colour.
I have added and customised the tableview programatically so I am looking for a programatic answer, as I did not use storyboard much for this. (I had only used it to setup my TabBarController and link it to navigation and view-controllers.)
Below is the some of the code I used to configure the tableview.
private let tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.register(ProfileTableViewCell.self, forCellReuseIdentifier: ProfileTableViewCell.identifier)
tableView.backgroundColor = Constants.backgroundColor
return tableView
}()
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = view.bounds
}
Im sure the answer is probably just a single line of code, but I couldn't find one that worked. Thanks in advance!
***EDIT
I have found a solution, but I still think there is a better way to solve this problem. The code below sets a UIView as the background of the tableview, then changes the colour of the UIView.
private let tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.register(ProfileTableViewCell.self, forCellReuseIdentifier: ProfileTableViewCell.identifier)
let backgroundView = UIView(frame: CGRect(x: 0,
y: 0,
width: tableView.bounds.size.width,
height: tableView.bounds.size.height))
backgroundView.backgroundColor = Constants.backgroundColor
tableView.backgroundView = backgroundView
return tableView
}()
The default color of the containing view controller is showing, since the table is short. Try setting the view controller's view's background color in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad();
view.backgroundColor = Constants.backgroundColor
}

Swift text field border isn't the right width

I have a bottom border that I generated after following the answer here.
This works absolutely great except the border isn't the right width. It's set with constraints to match the width of the button below it but as you can see is coming up short.
What am I missing?
Code :
extension UITextField
{
func setBottomBorder(withColor color: UIColor)
{
self.borderStyle = UITextBorderStyle.none
self.backgroundColor = UIColor.clear
let width: CGFloat = 3.0
let borderLine = UIView(frame: CGRect(x: 0, y: self.frame.height - width, width: self.frame.width, height: width))
borderLine.backgroundColor = color
self.addSubview(borderLine)
}
}
then in the VC :
override func viewDidLoad() {
authorNameOutlet.setBottomBorder(withColor: UIColor.lightGray)
}
Then Xcode shows...
but the simulator shows...
I've tried this both setting the width of the text field to be 0.7 x the superview width (same as the button below it) and also setting the width of the text field to be equal width of the button but neither works.
This is because of AutoLayout.
You can add autoresizingMask to your line.
borderLine.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
You are working with with a static frame for the border line view. After viewDidLoad your view controller's view gets resized.
Option 1: (Fast and dirty)
Move your code from viewDidLoad() to viewWillAppear(_ animated: Bool). viewWillAppear gets called after the first layout of your view controller's view
Option 2:
Add constraint for your border line view. So that your border line view will resize automatically.
Importent hint:
Do not forget super calls in overrides or you will get strange bugs!
E.g:
override func viewDidLoad() {
super.viewDidLoad()
// your code
}

NavigationBarItem misplaced at first app start

I have a strange issue with my NavigationBarItems. After first app start (when application was not running in background) the BarButton is misplaced (see Screenshot 1). The Button title should be "PDF".
Screenshot 1
However, when i press the home button and open up the app again (from background), the position is correct (Screenshot 2).
Screenshot 2
I can´t figure out what the problem is. I use a custom titleView for the navigation bar, which looks like that:
class TitleView : UIView {
var titleLabel:UILabel!
init(title:String) {
super.init(frame: CGRect(x: 0, y: 0, width: 200, height: 44))
titleLabel = UILabel()
titleLabel.textAlignment = .Center
titleLabel.font = UIFont.normalFont(15)
titleLabel.text = title.uppercaseString
titleLabel.textColor = UIColor.primaryColor()
self.addSubview(titleLabel)
titleLabel.snp_makeConstraints { (make) -> Void in
make.edges.equalTo(self.snp_edges)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension UIViewController {
func setTitleView(title:String) {
self.navigationItem.titleView = TitleView(title: title)
self.view.backgroundColor = UIColor.whiteColor()
}
And i initialize the navigationTitle and item in viewDidLoad as follows:
self.setTitleView("Tanzkarte")
let sendDanceCardButton = UIBarButtonItem(title: "PDF", style: .Plain, target: self, action: #selector(DanceCardController.sendDanceCard))
self.navigationItem.rightBarButtonItem = sendDanceCardButton
I didn`t find any solution for that problem in the internet and hope someone of you has a solution for it.
EDIT: the custom title view is not the issue. Even when I don´t use any title for the navigation bar, the button is misplaced.
Your title view is initialized with an explicit width which probably exceeds the maximum width allowed by the navigation bar. Try to init it with zero size, and call sizeToFit() after initialization.

Subclass of UIButton with rounded corners (swift)

Using swift, I'd like like to create a button with rounded corners. To make this re-usable, my preference is to subclass UIButton, and have come up with the following:
import Foundation
import UIKit
class LoginButton: UIButton {
let corner_radius : CGFloat = 4.0
override func drawRect(rect: CGRect) {
super.drawRect(rect)
self.layer.cornerRadius = corner_radius
}
}
Unfortunately, this doesn't seem to work as I had hoped, even though it compiles fine. Perhaps I'm missing something - I'm very new to this!
Any ideas?
You need to use clipsToBounds to ensure that the containing view isn't drawn over the corner radius:
self.clipsToBounds = true
You need also to turn on the masksToBounds on the layer:
self.layer.masksToBounds = true