In the notification center (iOS 7) it is possible to swipe between the "Today", "All", and "Missed" options of (what I presume is ) a segmented control. I would like to do this in my code but am unsure how to contiunue. I have a uisegmented control and a uitableviiew as my view.
I accomplished this by following the respective steps:
Add 2 swipe gesture recognisers(one to swipe left and the other to
swipe right)
For each of them on the connections inspector reference the outlet
collection as your main view.
Selecting actions when swiping as follows:
viewSelector is the UISegmentedControl and firstView, secondView and
thirdView are 3 views used to select which one is being shown.
Code:
#IBAction func indexChanged(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex
{
case 0:
firstView.hidden = false
secondView.hidden = true
thirdView.hidden = true
case 1:
firstView.hidden = true
secondView.hidden = false
thirdView.hidden = true
case 2:
firstView.hidden = true
secondView.hidden = true
thirdView.hidden = false
default:
break;
}
}
#IBAction func swipeLeft(sender: UISwipeGestureRecognizer) {
viewSelector.selectedSegmentIndex = (viewSelector.selectedSegmentIndex + 1) % viewSelector.numberOfSegments
indexChanged(viewSelector)
}
#IBAction func swipeRight(sender: UISwipeGestureRecognizer) {
viewSelector.selectedSegmentIndex = (viewSelector.selectedSegmentIndex - 1) % viewSelector.numberOfSegments
if(viewSelector.selectedSegmentIndex == -1){
viewSelector.selectedSegmentIndex = viewSelector.numberOfSegments-1
}
println(viewSelector.selectedSegmentIndex)
indexChanged(viewSelector)
}
This could be accomplished using a UIScrollView with paging enabled, and when the user scrolls through the pages, the UISegmentedControl's selectedSegmentIndex is updated.
It looks like it isn't a scrollView with paging enabled to me. I think it could be easier with a UIGestureRecognizer. If it recognises a gesture, slide the view of the current tab and update the segmented control.
Related
In my project, I enable a coacopods called 'SwipeableTabBarController'. These allow my tab bar view controller detect pan gestures and switch between tabs. And I also write some code to detect swipe gesture, which allows users to hide the tab bar.
Problem: My app will have a slide animation even when the user directly tap on the bar item. Any way to solve this? I appreciated any help!
Tried to disable swipe and pan gestures when a tap is detected. But the pan gestures are not in my gesture array.
Use isSwipeEnabled = false to disable the swipe feature. By default it is set to true in SwipeableTabBarController
UPDATE:
Since you are looking for a solution without the animation that SwipeableTabBarController library provides, but still want the swipe feature. Here is how you can do this with the default UITabBarController.
Step 1:
Create a default UITabBarController and 2 View Controllers, lets call them ViewController_1 & ViewController_2
Step 2:
Create a class for each ViewController and in the ViewDidLoad() method of both ViewController_1 & ViewController_2 add these lines.
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeRight.direction = UISwipeGestureRecognizer.Direction.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
self.view.addGestureRecognizer(swipeLeft)
}
And then add this function for every time a swipe is detected in both the classes.
#objc func swiped(_ gesture: UISwipeGestureRecognizer) {
if gesture.direction == .left {
if (self.tabBarController?.selectedIndex)! < 2
{
self.tabBarController?.selectedIndex += 1
}
} else if gesture.direction == .right {
if (self.tabBarController?.selectedIndex)! > 0 {
self.tabBarController?.selectedIndex -= 1
}
}
}
This will give you the ability to swipe and navigate to different ViewControllers and also navigate using the Tabbar buttons.
Hope this helps.
You can use the property of your POD isSwipeEnabled = false on your tap action
It will disable scroll animation when you tap on tabbar items.
i want to Switch UIView by clicking Segmented Control but this Code is not working.
#IBOutlet weak var flightTypeSegCont: UISegmentedControl!
#IBAction func flightType(_ sender: UISegmentedControl) {
if(flightTypeSegCont.selectedSegmentIndex == 0)
{
self.direcrCard.isHidden = false
self.ViaCardView.isHidden = true
}
else
{
self.direcrCard.isHidden = true
self.ViaCardView.isHidden = false
}
}
Did you check if the function is getting called in XCode debugger? If not, connect IBAction from storyboard / xib to your function - valuechanged callout of UISegmentedControl.
If it is being called but views are not hidden as you want, check if they are having parent/child relationship. To check this in more details, go to XCode->View Debugging->Capture View hierarchy option to see the runtime UIView layout.
I'm practicing creating an app that has 3 views. Each view has a button to hide itself. Now, I added a button outside the views to re-add them after I hide them, but, when i remove the views and I press it, it shows me all of the three views. I want it to add one view at a time (without removing the other views). What can I do?
This is my code, I was even thinking about adding multiple add buttons but it wouldn't be clear.
#IBAction func addViewButton(_ sender: Any) {
view1.isHidden = false
view2.isHidden = false
view3.isHidden = false
}
Declare a variable like currentVisibleViews in your ViewController, and lets say at first there is only first one is visible so we start off the number as 1, and also add your views to an array to control easier:
var currentVisibleViews = 1
var viewArray = [UIView]()
In viewDidLoad, add your views to viewArray:
view1.isHidden = false
view2.isHidden = true
view3.isHidden = true
viewArray.append(view1)
viewArray.append(view2)
viewArray.append(view3)
Then in your button:
#IBAction func addViewButton(_ sender: Any) {
if currentVisibleViews > 2 {
viewArray.forEach { (view) in
view.isHidden = true
}
currentVisibleViews = 0
} else {
viewArray[currentVisibleViews].isHidden = false
currentVisibleViews += 1
}
}
Code above works as this:
First all views are visible
First tap will hide all views
Then each tap will make a view visible
Go to step 2 (once all views are visible)
By this way, you can only have one function to control all your views, it doesnt have to be IBAction anymore, it can be just a method of your ViewController
Codes:
for ... {
let view = CategoryClass.createMyClassView()
view.myLabel.text = packTitle
view.twoLabel.text = packText
view.bgCaategory.layer.cornerRadius = 30
i = i + 1
if(i == 1){
selectPackId = packId!;
view.imgSelect.image = UIImage(named: "selected")
} else {
view.imgSelect.image = UIImage(named: "select")
}
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSendData(sender:))))
self.stackView.addArrangedSubview(view)
}
#objc func handleSendData(sender: UITapGestureRecognizer) {
print("H 1")
}
If i click on view, nothing print "H 1"
I want if i click on view, get id or another value of view
If adding isUserInteractionEnabled as suggested by Marcel still doesn't work, also make sure that every parent view in the hierarchy has a valid frame (you can check it in Debug View Hierarchy).
E.g. it happened to me to add a UIStackView into a parent UIView but the layout constraints were not correct, so I ended up having the parent UIView frame size as 0 (but the UIStackView was still visible).
If you create the UIStackView in interface builder, the isUserInteractionEnabled property is false by default. This means that the view and all it's child views won't respond to user interaction.
When you create a view in code, this property is true be default.
Add:
stackView.isUserInteractionEnabled = true
You only have to add this once, in your viewDidLoad for example.
The reason it doesn’t work is possibly a wrong method signature. The correct signature for recognizer actions is this:
recognizerAction(_ recognizer: UIGestureRecognizer)
I have seen lots of code to hide the whole tabbar. I am using tab bar controller in storyboard.
If I had third and fourth buttons how could I hide just the second but still have 1, 3 and 4 buttons shown?
Well just hide the button itself.
button2.hidden = true
You will need to create an outlet.
#IBOutlet var button2 : UIButton!
And link it to the button in Interface Builder
Ok to hide the button the following can be done. I used this code in a table view to add the editing function and change the title of the button based on the the click. But I modified it for this post just to show that when I clicked on the button again it would make it disappear.
var condition: Bool = true
#IBAction func buttonEdit(sender: UIBarButtonItem) {
if(condition == true) {
tableView.editing = true
sender.title = "Done"
condition = false
} else {
tableView.editing = false
sender.title = "" // This clears the title
sender.enabled = false // This disables the button
sender.image = UIImage() // This would clear the image
condition = true
}
}