How Replace Position++ Code to Make It Swift 3 Compatible

How replace position++ code to make it Swift 3 compatible?

defer can be used to increment the position variable
after the return value has been computed:

func getNextToken() -> Token? {
guard position < tokens.count else {
return nil
}
defer {
position += 1
}
return tokens[position]
}

UIView changing its position in swift

You have to implement an animation changing the DynView position on click. Here's an example:

@IBAction func btnUp(sender: AnyObject) {
let xPosition = DynView.frame.origin.x
let yPosition = DynView.frame.origin.y - 20 // Slide Up - 20px

let width = DynView.frame.size.width
let height = DynView.frame.size.height

UIView.animateWithDuration(1.0, animations: {
dynView.frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
})
}

How to change the button position during landscape mode in iOS?

Assuming you are already handling orientation and you receive events correctly I find it best if you create double constraints and manipulate their priority.

In storyboard place the button on one of the positions and add all constraints needed for it. Set all priorities to 900 (will explain later why not 1000) and confirm the layout is OK.

Now set these constraint priorities to 100 and start adding constraints for the other layouts. Set these constraints priorities to 900 and confirm the layout works for this scenario as well.

At this point you would need to change the priorities of one set to 900 and other to 100 to swap the mode. So now go into coding...

You will need outlets for these constraints which will be painful to drag all of them, name them and add then change the priority for each. But luckily you may use arrays for this so add the following in your code:

@IBOutlet var portraitConstraints: [NSLayoutConstraint]?
@IBOutlet var landscapeConstraints: [NSLayoutConstraint]?

Now simply drag all of the constraints into these 2 arrays as outlets.

The rest is pretty straight forward. Something like:

func setMode(mode: LayoutMode, animated: Bool) {
switch mode {
case .portrait:
portraitConstraints?.forEach { $0.priority = 900 }
landscapeConstraints?.forEach { $0.priority = 100 }
case .landscape:
portraitConstraints?.forEach { $0.priority = 100 }
landscapeConstraints?.forEach { $0.priority = 900 }
}
UIView.animate(withDuration: animated ? 0.3 : 0.0) {
self.view.layoutIfNeeded()
}
}

About not setting a constraint priority to 1000:

It is not allowed to change the priority of a constraint that has a priority set to 1000. Don't know why, don't care why.

EDIT: This seems to be unclear so let me give you an example from your case:

Assume you have a view that holds "Mobile", "1", "20.00KD", "-/+" called inputContainer, a delete button called deleteButton and image view on the left called imageView.

Now the portrait mode you would set the constraints as:

  • inputContainer.top = superView.top + t
  • inputContainer.leading = imageView.trailing + a
  • deleteButton.leading = inputContainer.leading
  • deleteButton.top = inputContainer.bottom + b
  • deleteButton.bottom = superView.bottom - t

Then in landscape you would have:

  • inputContainer.center.y = superView.center.y
  • inputContainer.leading = imageView.trailing + a
  • deleteButton.center.y = switchView.center.y
  • deleteButton.leading = inputContainer.trailing + b

Now set one of these to priority of 900 and others to 100 in storyboard. Then connect all of these constraints into appropriate arrays in your code. That is it.

How can i change Uipickerview position?

If you add something programmatically(calling addSubView:) you have to do autolayout programmatically in order to position your view in correct order.

There are lots of tutorials about this.I am just giving you some example about it.Suppose if you want to put your PickerView

Left position : 20px

Top position : 100px

You have to first write below line.Why? just google it. :)

pickerView.translatesAutoresizingMaskIntoConstraints = false

I am using anchoring.Because code is very much reduced.Thanks to Apple.

 pickerView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20.0).isActive = true
pickerView.topAnchor.constraint(equalTo:self.view.topAnchor, constant: 100.0).isActive = true

Of course you must add this after you add subView. remove this line from your code by the way.You can center align using autolayout. :)

pickerView.center = view.center

+=' produces '()'. How can i return an incremented value from a closure

Just another way, use defer to increment counter after return

func countingClosure() -> (() -> Int) {
var counter = 0
let incrementCounter: () -> Int = {
defer {
counter += 1
}
return counter
}
return incrementCounter
}

Y Position difference on different devices for custom separator line

Frames are not completely laid-out in viewDidLoad().

You could move your call to viewDidLayoutSubviews():

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
drawCalendarSeperatorLine()
}

however, viewDidLayoutSubviews() can, and very often will, be called multiple times (such as on device rotation), and your func will be adding a new sublayer every time.

Another (probably better approach) would be to add a 1-pt high view, constrained to the bottom of calendarView.

So, in viewDidLoad() you can do this:

override func viewDidLoad() {
super.viewDidLoad()

let sepView = UIView()
sepView.backgroundColor = UIColor.separator.withAlphaComponent(0.7)
sepView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(sepView)
NSLayoutConstraint.activate([
sepView.leadingAnchor.constraint(equalTo: calendar.leadingAnchor, constant: 15.0),
sepView.trailingAnchor.constraint(equalTo: calendar.trailingAnchor, constant: -15.0),
sepView.topAnchor.constraint(equalTo: calendar.bottomAnchor),
sepView.heightAnchor.constraint(equalToConstant: 1.0),
])
}

Now you'll have your "separator line" at the bottom of the calendar view, and it will "stick" to the bottom and resize its width if/when the calendar view frame changes.

How can I change the Title for a 3 segmented Control Segments, when I enter a view control?

Swift 3.0 use:

 @IBOutlet weak var entrada : UISegmentedControl!

override func viewDidLoad() {
super.viewDidLoad()

entrada.setTitle("Action 1", forSegmentAt: 0)
entrada.setTitle("Action 2", forSegmentAt: 1)
entrada.setTitle("Action 3", forSegmentAt: 2)
}


Related Topics



Leave a reply



Submit