IOS 6 to Swift2 update Slider - ios

any chance someone to help me in this code? it is a Slider that changes the size of the Fonts in a Label. this worked in IOS6 but I am trying to run on Swift 2.Thanks in advance.
import UIKit
class ViewController: UIViewController {
#IBAction func defaultSlider1(sender: UISlider) {
UISlider *slide = (UISlider *)sender;
int fonts = (int)(slide.value);
NSString *newtext = NSlog("%d", fonts);
label.font = [UIFont systemFontOfSize:fonts];
label.text = newtext;
}
override func viewDidLoad() {
super.viewDidLoad()
}
}

Please try following example (Demo):
import UIKit
class ViewController: UIViewController {
private var slider: UISlider!
private var label: UILabel!
private let minimumFontSize: Float = 16
private let maximumFontSize: Float = 48
override func viewDidLoad() {
super.viewDidLoad()
let width: CGFloat = view.bounds.width / 2
let height: CGFloat = 44.0
let size = CGSize(width: width, height: height)
let origin = CGPoint(x: self.view.center.x - (width / 2), y: self.view.center.y)
createSlider(origin, size: size)
createLabel(origin, size: size)
sliderAction()
}
func createSlider(origin: CGPoint, size: CGSize) {
let frame = CGRect(origin: origin, size: size)
self.slider = UISlider(frame: frame)
self.slider.addTarget(self,
action: Selector("sliderAction"),
forControlEvents: UIControlEvents.ValueChanged)
self.slider.minimumValue = minimumFontSize
self.slider.maximumValue = maximumFontSize
view.addSubview(self.slider)
}
func createLabel(origin: CGPoint, size: CGSize) {
let labelOrigin = CGPoint(x: origin.x, y: origin.y + size.height)
let frame = CGRect(origin: labelOrigin, size: size)
self.label = UILabel(frame: frame)
self.label.textAlignment = NSTextAlignment.Center
view.addSubview(self.label)
}
func sliderAction() {
let fontSize = CGFloat(self.slider.value)
self.label.font = UIFont.systemFontOfSize(fontSize)
self.label.text = "\(Int(fontSize))"
}
}
In case you're using Storyboards:
Add UISlider and UILabel to your View Controller Scene on the Storyboard
Insert #IBOutlets for each control (Ctrl+Drag from the control on the Storyboard to code, choose Outlet from Connection list)
Insert #IBAction for slider's Value Changed event (Ctrl+Drag from the slider control on the Storyboard to code, choose Action from Connection list)
You may end up with something like:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var slider: UISlider!
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.slider.minimumValue = 16
self.slider.maximumValue = 48
self.sliderAction()
}
#IBAction func sliderValueChangedAction(sender: UISlider) {
self.sliderAction()
}
private func sliderAction() {
let fontSize = CGFloat(self.slider.value)
self.label.font = UIFont.systemFontOfSize(fontSize)
self.label.text = "\(Int(fontSize))"
}
}

Related

Adjust popOverViewController to the size of the label.text

So i have a popover yet i cant manage to configure the right size:
So this is the popover class:
final class PopOverViewController: UIViewController {
#IBOutlet weak var lbl: UILabel!
var text: String?
init(text: String) {
super.init(nibName: "PopOverViewController", bundle: nil)
self.text = text
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
lbl.text = text
//lbl.sizeToFit()
// self.view.frame = lbl.frame
}
}
The label is align to x and y center.
The invoke is :
fileprivate func showTipIfNedded() {
let optionItemListVC = PopOverViewController(text: "qewlghsflgha;lgh;lkfgj")
optionItemListVC.modalPresentationStyle = .popover
optionItemListVC.view.backgroundColor = .red
if let popover = optionItemListVC.popoverPresentationController {
popover.sourceView = self.view
popover.permittedArrowDirections = .down
//popover.containerView?.backgroundColor = .red
guard let firstTab = tabBarController?.tabBar.items?[0].value(forKey: "view") as? UIView else { return }
popover.sourceRect = CGRect(x: firstTab.frame.midX, y: (self.tabBarController?.tabBar.frame.minY)!, width: 1, height: 1)
optionItemListVC.preferredContentSize = CGSize(width: optionItemListVC.lbl.frame.width, height: optionItemListVC.lbl.frame.height)
popover.delegate = self
}
self.present(optionItemListVC, animated: true, completion: nil)
}
With the current constraint the vc is small an i cant see the whole text, with constraint thet are 50 to all side the vc is to big, more then needed.
What constraints must i build? and are there more things i should config?
In your PopoverViewController override preferredContentSize to control the size of the view controller, something like this:
import UIKit
class PopoverViewController: UIViewController
{
#IBOutlet weak var lbl: UILabel!
override func viewDidLoad()
{
super.viewDidLoad()
...
}
// Preferred content size
override var preferredContentSize: CGSize
{
get
{
// measure
let maxLabel: CGSize = CGSize.init(width: lbl.frame.size.width, height: CGFloat.greatestFiniteMagnitude)
let fittingSize: CGSize = lbl.sizeThatFits(maxLabel)
// add some margins/padding, margins = 8 horizontal = 12 vertical
return CGSize.init(width: fittingSize.width + 16, height: fittingSize.height + 24)
}
set
{
super.preferredContentSize = newValue
}
}
}
The getter measures the size of the UILabel (assuming its already populated with text), adding some vertical and horizontal padding

Tap gesture on custom Marker View using iOS-Charts

I've created a custom marker view that loads from xib. I want to detect tap on the marker so that I can perform certain action on my VC. How can I do this?
PerformanceHomeViewController:
#IBOutlet weak var segmentBarChartView: SegmentBarChartView!
override func viewDidLoad() {
super.viewDidLoad()
self.segmentBarChartView.setupMarkerView(customText: "%# of 30 Agents")
}
SegmentBarChartView.swift
func setupMarkerView(customText: String) {
let customMarker: CustomMarker = (CustomMarker.viewFromXib() as? CustomMarker)!
customMarker.backgroundColor = .black
customMarker.setup(font: AIAMetrics.barChartTitleFont, textColor: .white, customString: customText)
customMarker.chartView = chartView
chartView.marker = customMarker
}
CustomMarkerView
#IBOutlet weak var textLabel: UILabel!
open var font: UIFont = .systemFont(ofSize: 12)
open var textColor: UIColor = .white
open var customString: String = ""
override open func awakeFromNib() {
self.offset.x = -self.frame.size.width / 2.0
self.offset.y = -self.frame.size.height - 7.0
}
public func setup(font: UIFont, textColor: UIColor, customString: String)
{
self.font = font
self.textColor = textColor
self.customString = customString
}
open override func refreshContent(entry: ChartDataEntry, highlight: Highlight) {
let labelString = String(format: customString, String(format: "%.0f", entry.y))
textLabel.text = labelString
layoutIfNeeded()
setLabel(labelString)
layoutIfNeeded()
}
open func setLabel(_ newLabel: String)
{
var size = CGSize()
size.width = textLabel.frame.size.width + 25 + 27
size.height = textLabel.frame.size.height + 20
size.height = max(size.height, 52)
self.frame.size = size
}
SegmentBarChartView is an xib that contains the Bar chart.
Can Anyone provide code example on what I should do so that I can detect tap on the marker view and perform some action on VC?

How to move an UIimageView random on the screen when you click the image

Can you help me please to move my image random on my View when I click it ?
I need to use UIView Animations (is required) and when the UIImageView is reaching the random destination on the screen it should change the color/image from inside.
Here is my code:
import UIKit
import QuartzCore
// ---- Is required to use UIView Animation ------
class FirstViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = UIImage(named: "1.png") // the second image is named "2.png"
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let touchLocation = touch.location(in: view)
let leftCorner = CGPoint(x: touchLocation.x + 48, y: touchLocation.y + 48)
imageView.center = leftCorner
}
}
Thanks in advance !
Here's my solution:
import UIKit
class ViewController: UIViewController {
let imageViewWidth = CGFloat(100)
let imageViewHeight = CGFloat(100)
let colors = [UIColor.red, UIColor.blue, UIColor.gray, UIColor.brown, UIColor.green]
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView.frame = CGRect(
x: view.bounds.width/2 - imageViewWidth/2,
y: view.bounds.height/2 - imageViewHeight/2,
width: imageViewWidth,
height: imageViewHeight
)
addTapRecognizerToImageView()
}
func addTapRecognizerToImageView() {
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap))
imageView.addGestureRecognizer(tap)
imageView.isUserInteractionEnabled = true
}
#objc func handleTap() {
let maxX = view.frame.maxX-imageViewWidth
let maxY = view.frame.maxY-imageViewHeight
let randomX = arc4random_uniform(UInt32(maxX)) + 0
let randomY = arc4random_uniform(UInt32(maxY)) + 0
UIView.animate(withDuration: 0.5) {
self.imageView.frame = CGRect(
x: CGFloat(randomX),
y: CGFloat(randomY),
width: self.imageViewWidth,
height: self.imageViewHeight
)
}
let randomColor = Int(arc4random_uniform(4) + 0)
imageView.backgroundColor = colors[randomColor]
}
}

change inputAccessoryView's height issue in iOS 8.4

I made a MessageComposerView like this
import UIKit
protocol MessageComposerDelegate: NSObjectProtocol {
func messageDidChange(messageView: MessageComposerView, height: CGFloat)
func postMessage(message: String)
}
private let maxTextViewHeight: CGFloat = 100.0
class MessageComposerView: UIView, UITextViewDelegate {
#IBOutlet weak var textView: SAMTextView!
#IBOutlet weak var sendButton: UIButton!
weak var delegate:MessageComposerDelegate?
#IBAction func sendButtonAction(sender: UIButton) {
delegate?.postMessage(textView.text)
}
override func awakeFromNib() {
let borderColor = UIColor(white: 0.9, alpha: 1).CGColor
let layer = CALayer()
layer.backgroundColor = borderColor
layer.frame = CGRectMake(0, 0, CGRectGetWidth(UIScreen.mainScreen().bounds), 1.0)
self.layer.addSublayer(layer)
textView.backgroundColor = UIColor.whiteColor()
textView.layer.borderColor = borderColor
textView.layer.borderWidth = 1.0
textView.layer.cornerRadius = 5.0
textView.delegate = self
textView.layoutManager.allowsNonContiguousLayout = false
sendButton.backgroundColor = Color.Red.colorValue
sendButton.tintColor = UIColor.whiteColor()
sendButton.layer.cornerRadius = 5.0
}
//MARK: TextViewDelegate
func textViewDidChange(textView: UITextView) {
self.sendButton.enabled = !self.textView.text!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).isEmpty
textView.scrollEnabled = textView.contentSize.height >= maxTextViewHeight
let height = textView.scrollEnabled ? textView.contentSize.height : textView.intrinsicContentSize().height
delegate?.messageDidChange(self, height: min(height, maxTextViewHeight) + 16.0)
if let selectedTextRange = self.textView.selectedTextRange {
let caretRect = self.textView.caretRectForPosition(selectedTextRange.end);
let height = self.textView.textContainerInset.bottom + caretRect.size.height
self.textView.scrollRectToVisible(CGRectMake(caretRect.origin.x, caretRect.origin.y, caretRect.size.width, height), animated: true)
}
}
}
and in the ViewController when I increase the height of MessageComposerView:
func messageDidChange(messageView: MessageComposerView, height: CGFloat) {
messageView.bounds.size.height = height
self.reloadInputViews()
messageView.textView.reloadInputViews()
}
In iOS 9,It works well.
but in iOS 8.4,The textView and Button not change their frame rightly
So What should I do to fix it in iOS 8.4,Thank you!

How to create a Scroll View with a page control using swift? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I am trying to create a Page View controller with numbers of view. I want a simple code sample that will use UIPageViewController.
import UIKit
class DummyVC: UIViewController, UIScrollViewDelegate {
let scrollView = UIScrollView(frame: CGRect(x:0, y:0, width:320,height: 300))
var colors:[UIColor] = [UIColor.red, UIColor.blue, UIColor.green, UIColor.yellow]
var frame: CGRect = CGRect(x:0, y:0, width:0, height:0)
var pageControl : UIPageControl = UIPageControl(frame: CGRect(x:50,y: 300, width:200, height:50))
override func viewDidLoad() {
super.viewDidLoad()
configurePageControl()
scrollView.delegate = self
scrollView.isPagingEnabled = true
self.view.addSubview(scrollView)
for index in 0..<4 {
frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
frame.size = self.scrollView.frame.size
let subView = UIView(frame: frame)
subView.backgroundColor = colors[index]
self.scrollView .addSubview(subView)
}
self.scrollView.contentSize = CGSize(width:self.scrollView.frame.size.width * 4,height: self.scrollView.frame.size.height)
pageControl.addTarget(self, action: #selector(self.changePage(sender:)), for: UIControlEvents.valueChanged)
}
func configurePageControl() {
// The total number of pages that are available is based on how many available colors we have.
self.pageControl.numberOfPages = colors.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.red
self.pageControl.pageIndicatorTintColor = UIColor.black
self.pageControl.currentPageIndicatorTintColor = UIColor.green
self.view.addSubview(pageControl)
}
// MARK : TO CHANGE WHILE CLICKING ON PAGE CONTROL
func changePage(sender: AnyObject) -> () {
let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width
scrollView.setContentOffset(CGPoint(x:x, y:0), animated: true)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
pageControl.currentPage = Int(pageNumber)
}
}
For lazy coder this is the Swift 3 implementation based on That lazy iOS Guy 웃's answer
import UIKit
class ViewController: UIViewController,UIScrollViewDelegate {
let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 320, height: 300))
var colors:[UIColor] = [UIColor.red, UIColor.blue, UIColor.green, UIColor.yellow]
var frame: CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
var pageControl : UIPageControl = UIPageControl(frame:CGRect(x: 50, y: 300, width: 200, height: 50))
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
configurePageControl()
scrollView.delegate = self
self.view.addSubview(scrollView)
for index in 0..<4 {
frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
frame.size = self.scrollView.frame.size
let subView = UIView(frame: frame)
subView.backgroundColor = colors[index]
self.scrollView .addSubview(subView)
}
self.scrollView.isPagingEnabled = true
self.scrollView.contentSize = CGSize(width: self.scrollView.frame.size.width * 4, height: self.scrollView.frame.size.height)
pageControl.addTarget(self, action: #selector(self.changePage(sender:)), for: UIControlEvents.valueChanged)
}
func configurePageControl() {
// The total number of pages that are available is based on how many available colors we have.
self.pageControl.numberOfPages = colors.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.red
self.pageControl.pageIndicatorTintColor = UIColor.black
self.pageControl.currentPageIndicatorTintColor = UIColor.green
self.view.addSubview(pageControl)
}
// MARK : TO CHANGE WHILE CLICKING ON PAGE CONTROL
func changePage(sender: AnyObject) -> () {
let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width
scrollView.setContentOffset(CGPoint(x: x,y :0), animated: true)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
pageControl.currentPage = Int(pageNumber)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Swift 3 - Horizontal image scroll
import UIKit
class pageenabled: UIViewController,UIScrollViewDelegate
{
let imagelist = ["img1.jpg", "photo1.jpg", "photo3.jpg", "photo4.jpg", "photo5.jpg"]
var scrollView = UIScrollView()
var pageControl : UIPageControl = UIPageControl(frame:CGRect(x: 50, y: 300, width: 200, height: 50))
var yPosition:CGFloat = 0
var scrollViewContentSize:CGFloat=0;
override func viewDidLoad() {
super.viewDidLoad()
scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300))
configurePageControl()
scrollView.delegate = self
self.view.addSubview(scrollView)
for i in stride(from: 0, to: imagelist.count, by: 1) {
var frame = CGRect.zero
frame.origin.x = self.scrollView.frame.size.width * CGFloat(i)
frame.origin.y = 0
frame.size = self.scrollView.frame.size
self.scrollView.isPagingEnabled = true
let myImage:UIImage = UIImage(named: imagelist[i])!
let myImageView:UIImageView = UIImageView()
myImageView.image = myImage
myImageView.contentMode = UIViewContentMode.scaleAspectFit
myImageView.frame = frame
scrollView.addSubview(myImageView)
}
self.scrollView.contentSize = CGSize(width: self.scrollView.frame.size.width * CGFloat(imagelist.count), height: self.scrollView.frame.size.height)
pageControl.addTarget(self, action: Selector(("changePage:")), for: UIControlEvents.valueChanged)
// Do any additional setup after loading the view.
}
func configurePageControl() {
// The total number of pages that are available is based on how many available colors we have.
self.pageControl.numberOfPages = imagelist.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.red
self.pageControl.pageIndicatorTintColor = UIColor.black
self.pageControl.currentPageIndicatorTintColor = UIColor.green
self.view.addSubview(pageControl)
}
// MARK : TO CHANGE WHILE CLICKING ON PAGE CONTROL
func changePage(sender: AnyObject) -> () {
let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width
scrollView.setContentOffset(CGPoint(x: x,y :0), animated: true)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
pageControl.currentPage = Int(pageNumber)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
In Swift 3.0
craeate iboutlets through story board for scrollview and pagecontroller.
#IBOutlet weak var imgScrollView: UIScrollView!
#IBOutlet weak var imgPageController: UIPageControl!
var sliderImagesArray = NSMutableArray()
in viewdidload method write this code
sliderImagesArray = ["https://images.unsplash.com/photo-1432679963831-2dab49187847?w=1080","https://images.unsplash.com/photo-1447746249824-4be4e1b76d66?w=1080", "https://images.unsplash.com/photo-1463595373836-6e0b0a8ee322?w=1080"]
imgScrollView.delegate = self
for i in 0..<sliderImagesArray.count {
var imageView : UIImageView
let xOrigin = self.imgScrollView.frame.size.width * CGFloat(i)
imageView = UIImageView(frame: CGRect(x: xOrigin, y: 0, width: self.imgScrollView.frame.size.width, height: self.imgScrollView.frame.size.height))
imageView.isUserInteractionEnabled = true
let urlStr = sliderImagesArray.object(at: i)
print(imgScrollView,imageView, urlStr)
imageView.sd_setImage(with: URL(string: urlStr as! String), placeholderImage: UIImage(named: "placeholder.png"))
imageView .contentMode = UIViewContentMode.scaleToFill
self.imgScrollView.addSubview(imageView)
}
self.imgScrollView.isPagingEnabled = true
self.imgScrollView.bounces = false
self.imgScrollView.showsVerticalScrollIndicator = false
self.imgScrollView.showsHorizontalScrollIndicator = false
self.imgScrollView.contentSize = CGSize(width:
self.imgScrollView.frame.size.width * CGFloat(sliderImagesArray.count), height: self.imgScrollView.frame.size.height)
imgPageController.addTarget(self, action: #selector(self.changePage(sender:)), for: UIControlEvents.valueChanged)
self.imgPageController.numberOfPages = sliderImagesArray.count
self.imgPageController.currentPage = 0
self.imgPageController.tintColor = UIColor.red
self.imgPageController.pageIndicatorTintColor = UIColor.black
self.imgPageController.currentPageIndicatorTintColor = UIColor.blue
after that implement scrollview delegate methods
func changePage(sender: AnyObject) -> () {
let x = CGFloat(imgPageController.currentPage) * imgScrollView.frame.size.width
imgScrollView.setContentOffset(CGPoint(x: x,y :0), animated: true)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = round(imgScrollView.contentOffset.x / imgScrollView.frame.size.width)
imgPageController.currentPage = Int(pageNumber)
}
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
#IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
#IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
#IBOutlet weak var imageViewTrailingConstraint: NSLayoutConstraint!
extension ZoomedPhotoViewController: UIScrollViewDelegate {
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
}
private func updateMinZoomScaleForSize(size: CGSize) {
let widthScale = size.width / imageView.bounds.width
let heightScale = size.height / imageView.bounds.height
let minScale = min(widthScale, heightScale)
scrollView.minimumZoomScale = minScale
scrollView.zoomScale = minScale
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
updateMinZoomScaleForSize(view.bounds.size)
}
private func updateConstraintsForSize(size: CGSize) {
let yOffset = max(0, (size.height - imageView.frame.height) / 2)
imageViewTopConstraint.constant = yOffset
imageViewBottomConstraint.constant = yOffset
let xOffset = max(0, (size.width - imageView.frame.width) / 2)
imageViewLeadingConstraint.constant = xOffset
imageViewTrailingConstraint.constant = xOffset
view.layoutIfNeeded()
}
func scrollViewDidZoom(scrollView: UIScrollView) {
updateConstraintsForSize(view.bounds.size)
}
import UIKit
public class PhotoCommentViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var nameTextField: UITextField!
public var photoName: String!
override public func viewDidLoad() {
super.viewDidLoad()
if let photoName = photoName {
self.imageView.image = UIImage(named: photoName)
}
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let cell = sender as? UICollectionViewCell,
indexPath = collectionView?.indexPathForCell(cell),
photoCommentViewController = segue.destinationViewController as? PhotoCommentViewController {
photoCommentViewController.photoName = "photo\(indexPath.row + 1)"
}
}
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: #selector(PhotoCommentViewController.keyboardWillShow(_:)),
name: UIKeyboardWillShowNotification,
object: nil
)
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: #selector(PhotoCommentViewController.keyboardWillHide(_:)),
name: UIKeyboardWillHideNotification,
object: nil
)
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func adjustInsetForKeyboardShow(show: Bool, notification: NSNotification) {
guard let value = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return }
let keyboardFrame = value.CGRectValue()
let adjustmentHeight = (CGRectGetHeight(keyboardFrame) + 20) * (show ? 1 : -1)
scrollView.contentInset.bottom += adjustmentHeight
scrollView.scrollIndicatorInsets.bottom += adjustmentHeight
}
func keyboardWillShow(notification: NSNotification) {
adjustInsetForKeyboardShow(true, notification: notification)
}
func keyboardWillHide(notification: NSNotification) {
adjustInsetForKeyboardShow(false, notification: notification)
}
#IBAction func hideKeyboard(sender: AnyObject) {
nameTextField.endEditing(true)
}
public var photoIndex: Int!
import UIKit
class ManagePageViewController: UIPageViewController {
var photos = ["photo1", "photo2", "photo3", "photo4", "photo5"]
var currentIndex: Int!
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
// 1
if let viewController = viewPhotoCommentController(currentIndex ?? 0) {
let viewControllers = [viewController]
// 2
setViewControllers(
viewControllers,
direction: .Forward,
animated: false,
completion: nil
)
}
}
func viewPhotoCommentController(index: Int) -> PhotoCommentViewController? {
if let storyboard = storyboard,
page = storyboard.instantiateViewControllerWithIdentifier("PhotoCommentViewController")
as? PhotoCommentViewController {
page.photoName = photos[index]
page.photoIndex = index
return page
}
return nil
}
}

Resources