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
Related
I want bView to be free of shadows. How can I achieve this?
I want to shadow all subviews of the superView, so I cannot remove the shadow settings of the superView.
let superView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
superView.layer.shadowColor = UIColor.lightGray.cgColor
superView.layer.shadowOffset = CGSize(width: 0, height: 3)
superView.layer.shadowOpacity = 0.2
superView.layer.shadowRadius = 5
self.view.addSubview(superView)
let aView: UIView = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
aView.backgroundColor = .white
superView.addSubview(aView)
let bView: UIView = UIView(frame: CGRect(x: 10, y: 230, width: 100, height: 100))
bView.backgroundColor = .white
superView.addSubview(bView)
I have tried bView.clipsToBounds = true but it's not working.
If you give superView a non-clear background color then none of its subviews will get a shadow. You can then apply a shadow to the subviews that you want to have a shadow.
let superView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 350))
superView.backgroundColor = .white // <-- Add this line
superView.layer.shadowColor = UIColor.black.cgColor
superView.layer.shadowOffset = CGSize(width: 0, height: 3)
superView.layer.shadowOpacity = 0.2
superView.layer.shadowRadius = 5
view.addSubview(superView)
let aView: UIView = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
aView.backgroundColor = .white
// Add shadow to aView
aView.layer.shadowColor = UIColor.black.cgColor
aView.layer.shadowOffset = CGSize(width: 0, height: 3)
aView.layer.shadowOpacity = 0.2
aView.layer.shadowRadius = 5
superView.addSubview(aView)
let bView: UIView = UIView(frame: CGRect(x: 10, y: 230, width: 100, height: 100))
bView.backgroundColor = .white
superView.addSubview(bView)
If you have several views that need a shadow then I would define a method to make it easier:
extension UIView {
func addShadow() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 0, height: 3)
layer.shadowOpacity = 0.2
layer.shadowRadius = 5
}
}
Then your code becomes something like this:
let superView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 350))
superView.backgroundColor = .white
superView.addShadow()
view.addSubview(superView)
let aView: UIView = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
aView.backgroundColor = .white
aView.addShadow()
superView.addSubview(aView)
let bView: UIView = UIView(frame: CGRect(x: 10, y: 230, width: 100, height: 100))
bView.backgroundColor = .white
superView.addSubview(bView)
I am adding images to scrollview but just getting one image in the scrollview, I dont know why I am not seing any other images but just empty scroll except one image
This is what I have tried
in viewdidload
imageScroll = UIScrollView(frame: CGRect(x: 10, y: 320, width: 230, height: 250))
imageScroll.contentSize = CGSize(width: 250, height: 2000)
imageScroll.backgroundColor = .white
in function
for (b,c) in zip(photoGotArray, 0..<totalPhotosGot) {
photoDataVarOk = UserDefaults.standard.object(forKey: b) as? NSData
newImageView = UIImageView(frame: CGRect(x: 10, y: 50, width: 230, height: 200))
newImageView.viewWithTag(c)
newImageView.image = UIImage(data: photoDataVarOk! as Data)
self.imageScroll.addSubview(newImageView)
}
here is the image with just one image and rest of the empty scrollview
You need to change the Y to be incrementing as according to your code all images have the same frame
let newImageView = UIImageView(frame: CGRect(x: 10, y: 50 + ( 200 * i ) , width: 230, height: 200))
set i as you want or from the loop parameters
//
for (c,b) in photoGotArray.enumerated() {
photoDataVarOk = UserDefaults.standard.data(forKey: b)
let newImageView = UIImageView(frame: CGRect(x: 10, y: 50 + ( 200 * c), width: 230, height: 200))
newImageView.viewWithTag(c)
newImageView.image = UIImage(data: photoDataVarOk!)
self.imageScroll.addSubview(newImageView)
}
let newImageView = UIImageView()
newImageView = UIImageView(frame: CGRect(x: 10, y: yourImageHight * (indexLoop), width: 230, height: 200))
in your loop
I have a text field where i have added a text and icon. Like screenshot below:
However i want to have space between the icon and the text..
I tried this code:
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
let image = UIImage(named: "username")
imageView.image = image
let viewLeft: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 20))
viewLeft.addSubview(imageView)
but got this:
how to add the space after the icon not before?
You can try below code :-
public func setLeftView(of image: UIImage!) {
//setting left image
self.paddingLeft = 50
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let paddingImage = UIImageView()
paddingImage.image = image
paddingImage.contentMode = .scaleAspectFit
paddingImage.frame = CGRect(x: 15, y: 0, width: 23, height: 40)
paddingView.addSubview(paddingImage)
self.leftView = paddingView
self.leftViewMode = UITextFieldViewMode.always
}
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
}
When i try to create an uiview with loading text and activityindicator, even though it looks centered in iphone 6, it is not centered in iphone 5.
How can i use auto layout for this programatically created UIView ?
I am trying to center this uiview in all screen sizes
boxView = UIView(frame: CGRect(x: TableView.frame.midX - 60, y: TableView.frame.midY - 15, width: 180, height: 50))
boxView.backgroundColor = UIColor.blackColor()
boxView.alpha = 0.5
boxView.layer.cornerRadius = 10
var activityView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
activityView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityView.startAnimating()
var textLabel = UILabel(frame: CGRect(x: 60, y: 0, width: 200, height: 50))
textLabel.textColor = UIColor.whiteColor()
textLabel.text = "Loading"
boxView.addSubview(activityView)
boxView.addSubview(textLabel)
TableView.addSubview(boxView)
EDIT: I tried this and it seems working good
var bounds: CGRect = UIScreen.mainScreen().bounds
var width:CGFloat = bounds.size.width
var height:CGFloat = bounds.size.height
boxView = UIView(frame: CGRect(x: width/2 - 90, y: height/2 - 100, width: 180, height: 50))