I have a horizontal scroll view and I have enabled paging option. So I have added total 3 image views in 3 pages "horizontally". I want my last page to display first. i.e. start from end right. Here is my code:
self.myScrollView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
let scrollViewWidth = self.myScrollView.frame.width
let scrollViewHeight = self.myScrollView.frame.height
let imgOne = UIImageView(frame: CGRectMake(0, 0, scrollViewWidth, scrollViewHeight))
let imgTwo = UIImageView(frame: CGRectMake(-(scrollViewWidth), 0, scrollViewWidth, scrollViewHeight))
let imgThree = UIImageView(frame: CGRectMake(-(scrollViewWidth)*2, 0, scrollViewWidth, scrollViewHeight))
imgOne.image = UIImage(named: "0")
imgTwo.image = UIImage(named: "1")
imgThree.image = UIImage(named: "2")
self.myScrollView.addSubview(imgOne)
self.myScrollView.addSubview(imgTwo)
self.myScrollView.addSubview(imgThree)
self.myScrollView.contentSize = CGSizeMake(scrollViewWidth*3, 0)
Initially set contentOffset to scrollViewWidth*2 which will scroll horizontally to two pages:
scrollView.contentOffset = CGPointMake(scrollViewWidth*2,0)
and you can set to whatever value you want in X value to scroll horizontally.
Related
I am not able to change the size of my image in the navigation bar for some reason.
Here is my code:
private func setupNavigationBarItems() {
let titleImageView = UIImageView(image: UIImage(named: "radius_image"))
titleImageView.frame = CGRect(x: 0, y: 0, width: 2, height: 2)
titleImageView.contentMode = .scaleAspectFit
navigationItem.titleView = titleImageView
}
It's as if the titleImageView.frame = CGRect(x: 0, y: 0, width: 2, height: 2) line isn't even working.
Haven't found any recent solutions that would help.
UIImageView comes on top of UINavigation Bar title view. In your case you are not changing frame of navigation bar title view.
Using a custom UIView & adding that instance on UINavigationBar item should solve your issue.
let titleView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 2, height: 2)) // Add your frames
let titleImageView = UIImageView(image: UIImage(named: "radius_image")) // Give your image name
titleImageView.frame = titleView.bounds
titleView.addSubview(titleImageView)
self.navigationItem.titleView = titleView
I'm calling a JSQMessageViewController and adding an image as the title but it's not centred due to the offset caused by the Back left-button.
Here's my code for adding the image:
let imageView = UIImageView()
imageView.frame.size.width = 40
imageView.frame.size.height = 40
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "avatar_example")
imageView.image = image
navigationItem.titleView = imageView
Thanks :)
Are you sure that the problem is in JSQMessageViewController? Maybe you just need to use standard sizes from title view (44*44) for alignment.
let imageView = UIImageView(image: UIImage(named: "avatar_example"))
imageView.contentMode = .scaleAspectFit
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
imageView.frame = titleView.bounds
titleView.addSubview(imageView)
self.navigationItem.titleView = titleView
You need to actually get the width and height of the UINavigationBar and center the image accordingly. Try this
guard let bar = navigationController.navigationBar else { return }
let bannerWidth = bar.frame.size.width
let bannerHeight = bar.frame.size.height
// centers image vertically & horizontally
let bannerX = bannerWidth / 2 - imageView.frame.width / 2
let bannerY = bannerHeight / 2 - imageView.frame.height / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = logoImageView
I'm trying to get a simple color picker by adding the colors as Images into an UIScrollView.
I've used UIScrollViews before and never encountered any issues. This time nothing gets displayed no matter what I do. Double checked if the colors get picked right and everything seems to be fine. For some reason the colors don't get added to the UIScrollView.
func fillColorPicker(colors: Array<String>){
for i in 0..<colors.count{
let imageView = UIImageView()
imageView.image = UIImage(named: colors[i])
imageView.contentMode = .scaleAspectFit
let xPosition = CGFloat(i) * imageView.frame.width
imageView.frame = CGRect(x: xPosition, y: 0, width: imageView.frame.width, height: self.colorPicker.frame.height)
colorPicker.contentSize.width = imageView.frame.width * CGFloat(i+1)
colorPicker.addSubview(imageView)
}
}
Found something. Somehow the imageView.frame.width is always 0. I assumed it would take the width from the image file?
Solution:
func fillColorPicker(colors: Array<String>){
for i in 0..<colors.count{
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 75, height: 75))
imageView.image = UIImage(named: colors[i])
imageView.contentMode = .scaleAspectFit
let xPosition = CGFloat(i) * imageView.frame.width
imageView.frame = CGRect(x: xPosition, y: 0, width: imageView.frame.width, height: self.colorPicker.frame.height)
colorPicker.contentSize.width = imageView.frame.width * CGFloat(i+1)
colorPicker.addSubview(imageView)
}
}
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
}
I'm creating a very simple detail view that has a few pictures the user can page through at the top, with some details on the item name on the bottom. However my scrollview does not page to the correct place, as so:
My scrollview's frame & the frame of its subviews are set as follows:
override func viewDidLoad() {
super.viewDidLoad()
self.pictures.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height / CGFloat(2))
self.pictures.clipsToBounds = false
let width = pictures.frame.width
let height = pictures.frame.height
/*
let view1 = UIView(frame: CGRectMake(pictures.frame.origin.x, 0, scrollViewWidth, scrollViewHeight))
let view2 = UIView(frame: CGRectMake(pictures.frame.origin.x + scrollViewWidth, 0, scrollViewWidth, scrollViewHeight))
let view3 = UIView(frame: CGRectMake(pictures.frame.origin.x + scrollViewWidth * 2, 0, scrollViewWidth, scrollViewHeight))
let view4 = UIView(frame: CGRectMake(pictures.frame.origin.x + scrollViewWidth * 3, 0, scrollViewWidth, scrollViewHeight))
*/
for index in 0...2 {
let frame = CGRectMake(width * CGFloat(index), 0, width, height)
let view = UIView(frame: frame)
views.append(view)
pictures.addSubview(view)
}
views[0].backgroundColor = UIColor.redColor()
views[1].backgroundColor = UIColor.blueColor()
views[2].backgroundColor = UIColor.orangeColor()
pictures.delegate = self
pictures.contentSize = CGSizeMake(width * CGFloat(views.count), height)
}
Does anyone have any suggestions as to how I could fit this? Am I implementing my scrollview incorrectly?
The problem is you trying to access self.view.frame in viewDidLoad() function. In the moment when it called views are not laid out properly yet. Frame of the view will be of size what you specified in storyboard, probably {0, 0, 600, 600}.
Solution: You may put your page-initializing code to viewWillAppear, or you may consider using Auto Layout instead of directly manipulating frames.
You have to add +20 in your width because in scrollView some space is available between two page.
So add 20 it may solve your problem.