Hide Labe at specific page on a scrollView swift 3 - ios

I have an UIScrollView and an UILabel on a ViewController. If the user arrives at the last page of the scrollView, the Label should be hidden. How can I do that? Here is my unfinished code:
swipeLabel.center.x = self.view.frame.width + 30
self.comingSoonLabel.isHidden = false
self.scrollView.frame = CGRect(x: 0, y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)
let scrollviewHeight = self.scrollView.frame.height
let scrollviewWidth = self.scrollView.frame.width
let imgOne = UIImageView(frame: CGRect(x: 0, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let imgTwo = UIImageView(frame: CGRect(x: scrollviewWidth, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let feedbackView = UIView(frame: CGRect(x: scrollviewWidth*4, y: 0, width: scrollviewWidth, height: scrollviewHeight))
imgOne.image = UIImage(named: "preview1")
imgTwo.image = UIImage(named: "preview2")
self.scrollView.addSubview(imgOne)
self.scrollView.addSubview(imgTwo)
self.scrollView.addSubview(feedbackView)
self.addChildFeedBack(feedbackView: feedbackView)
self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width * 3, height: self.scrollView.frame.height)
self.scrollView.isPagingEnabled = true
}
func addChildFeedBack(feedbackView: UIView){
let contestStoryboard = UIStoryboard(name: "feedback", bundle: nil)
let testVC = contestStoryboard.instantiateViewController(withIdentifier: "FeedbackViewController") as! FeedbackViewController
testVC.view.frame = feedbackView.bounds
feedbackView.addSubview(testVC.view)
self.addChildViewController(testVC)
testVC.didMove(toParentViewController: self)
self.comingSoonLabel.isHidden = true
}

You can get pageNumber using scrollview's delegate method.
Note -You need to assign delegate of scrollview and conform that delegate also.
scrollview.delegate = self //assign
class testClass: UIViewController, UIScrollViewDelegate // conform
Delegate
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
if pageNumber == 3{
// hide your label here
}
// do you logic related to last page
}

Related

How to change the postion of the title after changing the height of Navigation Bar in Xcode

I want to change the position of the image title of the navigation bar after changing the height of navigation, but it doesn't work. I'm a rookie,please help me, thank you guys!
override func viewDidAppear(_ animated: Bool)
{
NavigationBarHeight()
}
func NavigationBarHeight() {
for subview in (self.navigationController?.navigationBar.subviews)! {
if NSStringFromClass(subview.classForCoder).contains("BarBackground") {
var subViewFrame: CGRect = subview.frame
subViewFrame.size.height = 75
subview.frame = subViewFrame
}
}
}
func addNavBarImage() {
let navController = navigationController
let image = UIImage(named: "文字logo")
let imageView = UIImageView(image: image)
let bannerWidth = navController?.navigationBar.frame.size.width
let bannerHeight = navController?.navigationBar.frame.size.height
let bannerX = bannerWidth! / 2 - image!.size.width / 2
let bannerY = bannerHeight! / 2 - image!.size.height / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth!/2, height: bannerHeight! / 2)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
Well you can use this to change :-
func title_Logo() {
let imageView = UIImageView(image: #imageLiteral(resourceName: "icon-logo"))
imageView.frame = CGRect(x: 0, y: 0, width: 170, height: 30)
imageView.contentMode = .scaleAspectFit
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 170, height: 30))
titleView.addSubview(imageView)
titleView.backgroundColor = .clear
self.navigationItem.titleView = titleView
}
and just call the function where you want to use

Different sizes of scrollview pages

I like to set one page from my UIScrollView bigger than the others. The last page should be the same size like the whole UIView, is that possible?
That's my unfinished code:
self.scrollView.frame = CGRect(x: 0, y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)
let scrollviewHeight = self.scrollView.frame.height
let scrollviewWidth = self.scrollView.frame.width
let viewHeight = self.view.frame.height
let viewWidth = self.view.frame.width
let imgOne = UIImageView(frame: CGRect(x: 0, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let imgTwo = UIImageView(frame: CGRect(x: scrollviewWidth, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let imgTree = UIImageView(frame: CGRect(x: scrollviewWidth*2, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let feedbackView = UIView(frame: CGRect(x: scrollviewWidth*3, y: 0, width: viewWidth, height: viewHeight))
imgOne.image = UIImage(named: "preview1")
imgTwo.image = UIImage(named: "preview2")
imgTree.image = UIImage(named: "preview3")
self.scrollView.addSubview(imgOne)
self.scrollView.addSubview(imgTwo)
self.scrollView.addSubview(imgTree)
self.scrollView.addSubview(feedbackView)
self.addChildFeedBack(feedbackView: feedbackView)
self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width * 4, height: self.scrollView.frame.height)
self.scrollView.isPagingEnabled = true
The feedbackView is the one, which should be the biggest.
I don't know if this will give you the type of result you want, but something to try...
Instead of adding feedbackView to your scrollView, add another UIScrollView as the 4th subview, and then add feedbackView as a subview of that scroll view:
// your original setup code
self.scrollView.frame = CGRect(x: 0, y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)
let scrollviewHeight = self.scrollView.frame.height
let scrollviewWidth = self.scrollView.frame.width
let viewHeight = self.view.frame.height
let viewWidth = self.view.frame.width
let imgOne = UIImageView(frame: CGRect(x: 0, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let imgTwo = UIImageView(frame: CGRect(x: scrollviewWidth, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let imgTree = UIImageView(frame: CGRect(x: scrollviewWidth*2, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let feedbackView = UIView(frame: CGRect(x: scrollviewWidth*3, y: 0, width: viewWidth, height: viewHeight))
imgOne.image = UIImage(named: "preview1")
imgTwo.image = UIImage(named: "preview2")
imgTree.image = UIImage(named: "preview3")
self.scrollView.addSubview(imgOne)
self.scrollView.addSubview(imgTwo)
self.scrollView.addSubview(imgTree)
// don't add feedbackView to scrollView
// self.scrollView.addSubview(feedbackView)
// instead, create another scroll view,
// add feedbackView to that scroll view,
// and add that scroll view as the 4th "page" in scrollView
let fbScrollView = UIScrollView(frame: CGRect(x: scrollviewWidth*3, y: 0, width: scrollviewWidth, height: scrollviewHeight))
// just so we can see where it is during development
fbScrollView.backgroundColor = .purple
// feedbackView will now be in its own scroll view, so
// set its origin to 0,0
feedbackView.frame.origin = CGPoint.zero
fbScrollView.addSubview(feedbackView)
fbScrollView.contentSize = feedbackView.frame.size
// add the new scroll view to the "paging" scrollView
self.scrollView.addSubview(fbScrollView)
// and the rest of your setup
self.addChildFeedBack(feedbackView: feedbackView)
self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width * 4, height: self.scrollView.frame.height)
self.scrollView.isPagingEnabled = true

Check Landscape/Portrait orientation in an iMessage app (extension)

Have seen a lot of solutions to check the orientation, but strangely, none works!!
Below is the code snippet,
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
print("Screen Width = \(screenWidth)")
print("Screen Height = \(screenHeight)")
if (screenWidth > screenHeight) {
print("Landscape")
alertView.removeFromSuperview()
messageView.addGestureRecognizer(tapTextView)
}
else {
print("Portrait")
setupLandscapeAlertView()
}
}
The other method which is used to setup the view is,
fileprivate func setupLandscapeAlertView() {
messageView.removeGestureRecognizer(tapTextView)
alertView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
alertView.backgroundColor = UIColor.clear
alertView.isUserInteractionEnabled = false
let transparentView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.height, height: self.view.frame.width))
transparentView.backgroundColor = UIColor.gray
transparentView.backgroundColor = transparentView.backgroundColor!.withAlphaComponent(0.6)
transparentView.isUserInteractionEnabled = false
alertView.addSubview(transparentView)
let blurEffect = UIBlurEffect(style: .regular)
let blurredEffectView = UIVisualEffectView(effect: blurEffect)
blurredEffectView.frame = transparentView.bounds
blurredEffectView.isUserInteractionEnabled = false
alertView.addSubview(blurredEffectView)
let imageView = UIImageView(image: UIImage(named: "LandscapeAlert"))
imageView.frame = CGRect(x: 70, y: 75, width: imageView.frame.width, height: imageView.frame.height)
imageView.contentMode = UIViewContentMode.scaleAspectFit
imageView.contentMode = UIViewContentMode.center
imageView.isUserInteractionEnabled = false
alertView.addSubview(imageView)
self.view.addSubview(alertView)
}
Another thing is that, this image isn't centered. How do I go about that? Again, too many solutions, but nothing does the job.
Not sure if I'm doing anything wrong. :-/
This may be because viewWillTransition executes before the view actually flips. If you do a simple print("height width \(view.frame.height) \(view.frame.width)") inside the viewWillTransition block you will see.

Image size in a ScrollView

I have a ScrollView with pictures. Since the transition to Xcode8 / Swift 3, the image no longer fits the size of ScrollView. Inside the image, keeps its original size when in Xcode 7/2 swift image which was perfectly adapted to scroll and matched perfectly. Below is the corresponding code:
import UIKit
class GoaLawahViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var menuButton: UIBarButtonItem!
#IBOutlet weak var ScrollView: UIScrollView!
#IBOutlet weak var PageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
let ScrollViewWidth:CGFloat = self.ScrollView.frame.width
let scrollViewHeight:CGFloat = self.ScrollView.frame.height
let imgOne = UIImageView(frame: CGRect(x: 0, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgOne.image = UIImage(named: "GoaLawah04.jpg")
let imgTwo = UIImageView(frame: CGRect(x: ScrollViewWidth, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgTwo.image = UIImage(named: "GoaLawah07.jpg")
let imgThree = UIImageView(frame: CGRect(x: ScrollViewWidth*2, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgThree.image = UIImage(named: "GoaLawah09.jpg")
self.ScrollView.addSubview(imgOne)
self.ScrollView.addSubview(imgTwo)
self.ScrollView.addSubview(imgThree)
self.ScrollView.contentSize = CGSize(width: self.ScrollView.frame.width * 3, height: 1.0)
self.ScrollView.delegate = self
self.PageControl.currentPage = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
// Test the offset and calculate the current page after scrolling ends
let pageWidth:CGFloat = scrollView.frame.width
let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1
// Change the indicator
self.PageControl.currentPage = Int(currentPage);
}
}
try to add your imageViews on a containerView(just a new blank UIView),then add the containerView on your scrollView
Attached is my new code and it works. against that by not understand is that I was forced to remove my position constraints for it to function properly
import UIKit
class PandangBaiViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var PageControl: UIPageControl!
#IBOutlet weak var ScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let ScrollViewWidth:CGFloat = self.ScrollView.frame.width
let scrollViewHeight:CGFloat = self.ScrollView.frame.height
let imgOne = UIImageView(frame: CGRect(x: 0, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgOne.image = UIImage(named: "1PadangBay.jpg")
imgOne.contentMode = UIViewContentMode.scaleAspectFit
self.ScrollView.addSubview(imgOne)
let imgTwo = UIImageView(frame: CGRect(x: ScrollViewWidth, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgTwo.image = UIImage(named: "2PadangBay.jpg")
imgTwo.contentMode = UIViewContentMode.scaleAspectFit
self.ScrollView.addSubview(imgTwo)
let imgThree = UIImageView(frame: CGRect(x: ScrollViewWidth*2, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgThree.image = UIImage(named: "PadangBaiinternet1.jpg")
imgThree.contentMode = UIViewContentMode.scaleAspectFit
self.ScrollView.addSubview(imgThree)
let imgFour = UIImageView(frame: CGRect(x: ScrollViewWidth*3, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgFour.image = UIImage(named: "PadangBaiinternet2.jpg")
imgFour.contentMode = UIViewContentMode.scaleAspectFit
self.ScrollView.addSubview(imgFour)
let imgFive = UIImageView(frame: CGRect(x: ScrollViewWidth*4, y: 0,width: ScrollViewWidth, height: scrollViewHeight))
imgFive.image = UIImage(named: "PadangBaiinternet3.jpg")
imgFive.contentMode = UIViewContentMode.scaleAspectFit
self.ScrollView.addSubview(imgFive)
self.ScrollView.contentSize = CGSize(width: ScrollViewWidth * 5, height: scrollViewHeight)
self.ScrollView.isPagingEnabled = true
self.PageControl.currentPage = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
// Test the offset and calculate the current page after scrolling ends
let pageWidth:CGFloat = scrollView.frame.width
let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1
// Change the indicator
self.PageControl.currentPage = Int(currentPage);
}
}

IOS, Swift: creating a split screen with 2 views, with animation

I'm an absolute IOS newbie studying Swift.
I'm trying to create an app with a calendar UI control on top (already got that working), and a listview on the bottom.
Exactly like the picture on the bottom (and I apologise for the huge pic).
Now, like the calendar app, the screen will only show the calendar control, but if you click a button - the listview will reveal itself on the bottom and the screen will become split.
The only way I can think of doing this is to generate the UITableView in code, assigning the current ViewController as both delegate and datasource, and animating both the views (calendar and listview) to grow/shrink to split the screen.
I don't want to use StackView because that's IOS-9 and up.
Is there a better way to do this?
I suspect you're trying to do something like this. Hope it helps:
Swift 2:
let view2 = UIView()
var screenWidth = CGFloat()
var screenHeight = CGFloat()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenSize: CGRect = UIScreen.mainScreen().bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let view1 = UIView()
view1.frame = CGRectMake(0, 0, screenWidth, screenWidth)
view1.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.5)
self.view.addSubview(view1)
let lbl1 = UILabel()
lbl1.frame = CGRectMake(0, 0, screenWidth, 50)
lbl1.text = "This is view 1"
lbl1.textAlignment = NSTextAlignment.Center
lbl1.center = view1.center
view1.addSubview(lbl1)
let btn = UIButton()
btn.frame = CGRectMake(0,0,200,100)
btn.center = CGPoint(x: view1.center.x, y: view1.center.y+60)
btn.setTitle("Show view 2", forState: .Normal)
btn.addTarget(self, action: "showView2:", forControlEvents: .TouchUpInside)
view1.addSubview(btn)
view2.frame = CGRectMake(0, screenHeight, screenWidth, screenHeight-screenWidth)
view2.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5)
self.view.addSubview(view2)
let lbl2 = UILabel()
lbl2.frame = CGRectMake(0, (screenHeight-screenWidth)/2-25, screenWidth, 50)
lbl2.text = "This is view 2"
lbl2.textAlignment = NSTextAlignment.Center
view2.addSubview(lbl2)
}
var c = 0
func showView2(sender : UIButton) {
UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: {
if (self.c%2 == 0) {
self.view2.frame = CGRectMake(0, self.screenWidth, self.screenWidth, self.screenHeight-self.screenWidth)
sender.setTitle("Hide view 2", forState: .Normal)
}
else {
self.view2.frame = CGRectMake(0, self.screenHeight, self.screenWidth, self.screenHeight-self.screenWidth)
sender.setTitle("Show view 2", forState: .Normal)
}
}, completion: { finished in
})
c++
}
Swift 3:
let view2 = UIView()
var screenWidth = CGFloat()
var screenHeight = CGFloat()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenSize: CGRect = UIScreen.main.bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let view1 = UIView()
view1.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenWidth)
view1.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
self.view.addSubview(view1)
let lbl1 = UILabel()
lbl1.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 50)
lbl1.text = "This is view 1"
lbl1.textAlignment = NSTextAlignment.center
lbl1.center = view1.center
view1.addSubview(lbl1)
let btn = UIButton()
btn.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
btn.center = CGPoint(x: view1.center.x, y: view1.center.y+60)
btn.setTitle("Show view 2", for: .normal)
btn.addTarget(self, action: #selector(showView2(_:)), for: .touchUpInside)
view1.addSubview(btn)
view2.frame = CGRect(x: 0, y: screenHeight, width: screenWidth, height: screenHeight-screenWidth)
view2.backgroundColor = UIColor.red.withAlphaComponent(0.5)
self.view.addSubview(view2)
let lbl2 = UILabel()
lbl2.frame = CGRect(x: 0, y: (screenHeight-screenWidth)/2-25, width: screenWidth, height: 50)
lbl2.text = "This is view 2"
lbl2.textAlignment = NSTextAlignment.center
view2.addSubview(lbl2)
}
var c = 0
func showView2(_ sender : UIButton) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveEaseOut, animations: {
if (self.c%2 == 0) {
self.view2.frame = CGRect(x: 0, y: self.screenWidth, width: self.screenWidth, height: self.screenHeight-self.screenWidth)
sender.setTitle("Hide view 2", for: .normal)
}
else {
self.view2.frame = CGRect(x: 0, y: self.screenHeight, width: self.screenWidth, height: self.screenHeight-self.screenWidth)
sender.setTitle("Show view 2", for: .normal)
}
}, completion: { finished in
})
c += 1
}

Resources