i am new to swift and here i have a requirement i want to increase/ decrease navigation bar height as per requirement
Here: To increase navigationbar height i used this lines
extension UINavigationBar {
override open func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.size.width, height: 99.0)
}
}
this lines are working .but how to decrease navigationbar height
Thank you in advance
You can use this sample project.
https://developer.apple.com/library/content/samplecode/NavBar/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007418
Why don't you just simply set the frame for your navigationBar?
let size = CGSize(width: UIScreen.main.bounds.size.width, height: 100)
navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
Related
I'm creating a subview to add onto an existing view. I'm trying to assign the background color to this subview to red programmatically but displays as the defaulted color still.
let toggleView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
toggleView.backgroundColor = UIColor.red
As from the above code snippet it looks like that you are creating a view but not adding it as a subview to the parent view. It will be best if you can provide full function so that we can look into the actual cause.
You have created the toggleView with a red background but you have not added it to the main view, this is what you should do:
let screenWidth = view.frame.size.width
let screenHeight = view.frame.size.height
let toggleView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
toggleView.backgroundColor = UIColor.red
view.addSubview(toggleView)
I want a NSTextField with text direction as
how could I achieve this? thanks in advance for any help.
Choose orientation to VerticalĀ and then set text.
The input must be look like that.
I missed the NSTextFieldpoints so did it with NSTextView thanks to comment of #Willeke.
Cocoa framework has some points to rotate UI components.
For your NSTextField
myTextField.rotate(byDegrees: 90)
set your width & height carefully.
I rotate the NSTextField
nameTF.rotate(byDegrees: 270)
calculate the new height and width of NSTextField
let newHeight = nameTF.bestHeight(for: nameTF.stringValue, width: nameTF.frame.width)
let newWidth = nameTF.bestWidth(for: nameTF.stringValue, height: nameTF.frame.height)
set the new frame of NSTextField
nameTF.frame = CGRect(x: nameTF.frame.origin.x, y: self.view.frame.height - newWidth - 30, width: newHeight, height: newWidth)
used the extension of NSTextField
extension NSTextField {
func bestHeight(for text: String, width: CGFloat) -> CGFloat {
stringValue = text
let height = cell!.cellSize(forBounds: NSRect(x: 0, y: 0, width: width, height: .greatestFiniteMagnitude)).height
return height
}
func bestWidth(for text: String, height: CGFloat) -> CGFloat {
stringValue = text
let width = cell!.cellSize(forBounds: NSRect(x: 0, y: 0, width: .greatestFiniteMagnitude, height: height)).width
return width
}
}
so I have a table view which is the right size of my screen but the height is wrong because I cann't scroll down to see more parts/cells.
I have set the height in this code:
tableView.frame = CGRect(x: 0, y: 75, width: self.view.frame.width, height: self.view.frame.height)
should I just make the height a really big number or is there a way so that when more cells are filled I can scroll down?
tableView.frame = CGRect(x: 0, y: 75, width: self.view.frame.width, height: self.view.frame.height - 75)
You have add y position at 75. so remove 75 from screen height.
In my UINavigationBar subclass I am adding a subView with insertSubview(customView, at: 0) in order to place it in the very 'back' of the UINavigationBar.
In iOS 10 the added subView will be added the Title and UIBarButtonItems as I want it, but in in iOS 11 it seems like Apple changed something because no matter what I try the customView will be placed every time in the front of the UINavigationBar.
//containerView
containerView.frame = CGRect(x: 0, y: -(statusBarHeight), width: UIScreen.main.bounds.width, height: frame.height + statusBarHeight)
containerView.clipsToBounds = true
insertSubview(containerView, at: 0)
I already tried changing the index.
Any idea how I should tackle that problem?
You can look at this. I solved it this way:
override func layoutSubviews() {
super.layoutSubviews()
for aView in self.subviews {
if NSStringFromClass(type(of: aView)) == "_UINavigationBarContentView" {
aView.frame = CGRect(x: 0, y: 20, width: aView.frame.width, height: aView.frame.height)
}
else if NSStringFromClass(type(of: aView)) == "_UIBarBackground" {
aView.frame = CGRect(x: 0, y: 0, width: aView.frame.width, height: self.frame.height)
}
}
}
I am currently working on an app that has been developed using storyboards.
I am trying to add a view controller programmatically into a paging scroll view at the beginning before 2 other views that are already added within the storyboard.
The view controller gets added but the width is slightly larger and goes across into the middle view.
let vc = Vc()
scrollView.addSubview(vc.view)
You have to assign a contentSize to the scrollview and a position for the frame of the ViewController
scrollView.contentSize = CGSize(width: 2 * view.frame.width, height: scrollView.frame.height)
let frameVC = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
vc.view.frame = frameVC
vc.willMoveToParentViewController(self)
self.addChildViewController(vc)
vc.didMoveToParentViewController(self)
scrollView.addSubview(vc.view)
Note that you have to change the frameVC to match your requirements
Swift 5 sytax update
scrollView.contentSize = CGSize(width: 2 * view.frame.width, height: scrollView.frame.height)
let frameVC = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
vc.view.frame = frameVC
vc.willMove(toParent: self)
addChild(vc)
vc.didMove(toParent: self)
scrollView.addSubview(vc.view)
Note: Frames vs Constraint layout
Instead of using a frame you can also use create a constraint layout to add a UIView to a UIScrollView
scrollView.contentSize = CGSize(width: 2 * view.frame.width, height: scrollView.frame.height)
scrollView.addSubview(vc.view)
vc.view.translatesAutoresizingMaskIntoConstraints = false
// add constrains here
vc.willMove(toParent: self)
addChild(vc)
vc.didMove(toParent: self)