how to hide textfield and auto layout the constraint in swift - swift

so i have a segmented control in my project.I want to hide and auto layout my button.But the problem if i just hide the textfields and labels the button under my segmented control still not moving.So there is a space between my segmented control and next button.I want to move my next button up if i click no on my segmented control.
-if i click yes on segmented control
The next button under my segmented control will constraint my textfield and show all textfields and labels under my segmented control
-if i click no on segmented control
The next button under my segmented control will constraint to my segmented control and hide all textfields and labels under my segmented control
thanks for all the answers
the image

A quick example of how you could change the position of the button depending on the segment switch.
Below image shows dragging the auto layout height between Segment switch and button
Below Image shows using outlet and type needs to be NSLayoutConstraint
In your ViewController.Swift the code should look something like below.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var switchControl: UISegmentedControl!
#IBOutlet weak var heighConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func switchPressed(_ sender: Any) {
switch switchControl.selectedSegmentIndex
{
case 0:
// here you can see the constant is being set, this will determine the
// position of your button. You will need to set the correct position.
heighConstraint.constant = 150
case 1:
heighConstraint.constant = 200
default:
break;
}
}
}
Hope this answer helps.

Related

Swift 5 Segmented Control Truncates the Titles

I am using dynamic segmented control. Whenever user adds new decks, the title is showing up in the segmented control.
However, I need a horizontally scrollable segmented controller that might have different title width depending on the text (title) size.
this helped me to achieve it. iOS 13 Segmented Control: Remove swipe gesture to select segment
I added my segmented controller inside of scroll view.
Then changed my segmented controller class to the following;
class NoSwipeSegmentedControl: UISegmentedControl {
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}

How to hide input accessory view?

I have a segmented control in my application where one of the tabs is a chat controller.
I'm using the input accessory view for the toolbar that has the send button and the textfield.
The problem is i can't hide the input accessory view while switching tabs
I tried hiding it, removing it and adjusting the collection view bottom constraint accordingly when it's hidden or not but that didn't work
override var inputAccessoryView: UIView
{
return inputContainerView
}
override func canBecomeFirstResponder() -> Bool {
return true
}

Cannot modify NSTabViewItem

I may be getting lost in a glass of water as I am not an experienced developer but I cannot seem to be able to implement a simple override to modify the size of an NSTabView item.
I have a Tab View Controller (Style = toolbar)
I have a Tabless Tab View
I have 3 Tab Items. For testing I have only subclassed one of them to the subclass below
I have created a new subclass of NSTabViewItem: MyTabViewItem and subclassed one of the 3 tab Items. The code is:
import Cocoa
class MyTabViewItem: NSTabViewItem {
override func drawLabel(_ shouldTruncateLabel: Bool, in labelRect: NSRect) {
var size = self.sizeOfLabel(false)
size.width = 180
print("Draw!!")
}
override func sizeOfLabel(_ computeMin: Bool) -> NSSize {
var size = super.sizeOfLabel(false)
size.width = 180
print("Draw!!")
return size
}
}
Everything works, except the subclassing. The Tabs appear, they do operate by switching the views and the program runs as it should. Except that it does not resize the Tab Item. The code in the subclass MyTabViewItem is never reached (it never prints Draw!! as it should.
I cannot understand what I am missing here. I have not read of any IB connection to make (and I cannot seem to be able to connect the Tab Items anyways). Please apologise if it isa trivial question but I have searched everywhere and not found anything to help me.
Thank you
You said:
I have a Tabless Tab View
This is your problem. An NSTabView only asks an NSTabViewItem to drawLabel if the NSTabView itself is responsible for drawing the tab bar, but you have a “Tabless” tab view. (“Tabless” is the default style when you drag an NSTabViewController into a storyboard.)
You also said:
I have a Tab View Controller (Style = toolbar)
So you don't even want the tab view to draw a tab bar; you want items in the window toolbar to select tabs (like in Xcode's preference window).
Your ability to customize the toolbar items created for your tabs is limited. You can subclass NSTabViewController and override toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:, like this:
override func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
let toolbarItem = super.toolbar(toolbar, itemForItemIdentifier: itemIdentifier, willBeInsertedIntoToolbar: flag)
if
let toolbarItem = toolbarItem,
let tabViewItem = tabViewItems.first(where: { ($0.identifier as? String) == itemIdentifier.rawValue })
{
toolbarItem.label = "\(tabViewItem.label) 😀"
}
return toolbarItem
}
But I found that making other changes didn't work well:
Setting toolbarItem.image didn't work well for me.
Setting toolbarItem.view made the item stop receiving clicks.
Note that the minSize and maxSize properties are only used if toolbarItem.view is set.
Your best bet is probably to manage the toolbar yourself, without trying to use NSTabViewController's support.
I have also subclassed the NSTabViewController as follows:
import Cocoa
class MyTabViewController: NSTabViewController {
#IBOutlet weak var TradingTabItem: MyTabViewItem!
override func viewDidLoad() {
super.viewDidLoad()
print("Loaded Tab View")
TradingTabItem.label = "New"
// Do view setup here.
}
}
What happens now is that the tab item in my subclass (the only one of the 3 I subclassed) does change its label string to New. However, even if I have added the item as an IBOutlet here, it still does not change seize (and the overridden sizeOfLabel function is not reached).

Modal Panel with rounded corners

I'm wondering if there is a way to display a modal window with rounded corners instead of the default sharp corners. The image shows what corners I'm referring to.
I tried with changing the contentView.layer?.cornerRadius but it didn't work. What can I do to get the result I need?
After working a lot on it, I finally found a solution:
Frist create a new borderless window in your interface builder. Place a custom box in that view and make sure it leaves a bit of space from the top border of the window:
Then add an outlet of that window object in your app delegate:
#IBOutlet weak var saveWindow: NSWindow!
So copy that extension for loading and dismissing a panel as a modal sheet:
extension NSWindow {
public func loadPanel(named: NSWindow) {
named.isOpaque = false
named.backgroundColor = NSColor.clear
named.hasShadow = false
self.beginSheet(named, completionHandler: nil)
}
public func closePanel(named: NSWindow) {
self.endSheet(named)
}
}
You just need to call this two functions if you want to open a panel.
window.loadPanel(named: saveWindow)
And when you're done:
window.closePanel(named: saveWindow)
This is the result:

What is the syntax for editing a label's constraints/position in swift?

I want to set up a conditional that will change the location of a particular label in the view in swift. I figure I will put the change of the label's position in the viewDidLoad(), but if I put in this modification to move the label's constraints based on the conditional, will it (upon being reloaded) have the label in the position/constraint set manually on the storyboard? Or would I need to code that position back in?
if condition x{
//move label position
} else{
//keep the position set in storyboard, even upon reload
}
Is this possible?
///I have created the button & label on storyboard. And changing the position of label when button is clicked. I have added the constraint to the label on storyboard & created the outlet of top constraint.
var flag = true
#IBOutlet weak var UiLblConstraintTop: NSLayoutConstraint!
#IBAction func ChangePositionOfLabel(sender: UIButton) {
if flag{
self.UiLblConstraintTop.constant = 300
}
else{
self.UiLblConstraintTop.constant = 50
}
flag = !flag
self.view.layoutIfNeeded()
}