How to create custom uiview that button can be clicked inside uiview? - ios

I tried to create clicked scrollview button like this (at bottom):
this is my code so far:
scView = UIScrollView(frame: CGRect(x: 0, y: view.frame.maxY-110, width: view.bounds.width, height: 110))
self.view.addSubview(scView)
scView.backgroundColor = UIColor.lightGray
scView.translatesAutoresizingMaskIntoConstraints = false
for i in 0 ... 10 {
let myNewView=UIView(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100))
myNewView.backgroundColor=UIColor.white
myNewView.layer.cornerRadius=10
self.scView.addSubview(myNewView)
let label = UILabel(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding)+5, width: 150, height: 10))
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .left
label.text = "I'am a title label"
label.textColor = .black
myNewView.addSubview(label)
let ditancelabel = UILabel(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding)+5, width: 50, height: 10))
ditancelabel.center = CGPoint(x: 160, y: 285)
ditancelabel.textAlignment = .right
ditancelabel.text = "0.04 km"
ditancelabel.textColor = .red
myNewView.addSubview(ditancelabel)
let textView = UITextView(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding)+15, width: 200.0, height: 40.0))
self.automaticallyAdjustsScrollViewInsets = false
textView.center = self.view.center
textView.textAlignment = NSTextAlignment.justified
textView.textColor = UIColor.lightGray
textView.backgroundColor = UIColor.clear
myNewView.addSubview(textView)
let button = UIButton()
button.tag = i
button.backgroundColor = UIColor.white
button.setTitle("\(i)", for: .normal)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = UIColor.clear
button.addTarget(self, action:#selector(handleRegister(sender:)), for: .touchUpInside)
button.frame = CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100)
xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width
myNewView.addSubview(button)
}
scView.contentSize = CGSize(width: xOffset, height: scView.frame.height)
but code result like this:
The first button can be clicked, but the other cannot be clicked. And 2 UILabels and 1 UITextView did not appear.
the handleRegister method is to get sender tag and print it as log to know the button can be clicked or not.
How to repair the code so it can be like the custom UIView in the above image?
I need to transfer label and textview text by button click to another viewcontroller which handle by seque in handleRegister.

1) You can't see labels and other control because of your container view size is (200, 100)
let myNewView=UIView(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100))
and you set center of controls.
label.center = CGPoint(x: 160, y: 285)
ditancelabel.center = CGPoint(x: 160, y: 285)
This center point is not visible within container view. That's why you can't see labels within view. Just comment these line to set center of labels and you can see those perfectly.
2) I need to transfer label and textview text by button click to another viewcontroller which handle by seque in handleRegister.
I hope you have a array in which you stored data to display in your custom view, Just find that object from array based on button tag, and pass that object to another control.
3) The first button can be clicked, but the other cannot be clicked.
Again problem is same like for labels. You had set frame of button like this
button.frame = CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100)
and increase xOffset by this line to create another view
xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width
xOffset increased each time with the size of button. So if you print xOffset value it actually print like (assuming initial value of xOffset = 10, padding = 0)
for i in 0 ... 10 {
print(xOffset)
..... your other code
}
Output: will be like
10, 210, 410, 610...... Because of this your button is out of bound from container view and because of that you can't click on that.
I have updated your code, check below
scView = UIScrollView(frame: CGRect(x: 0, y: view.frame.maxY-110, width: view.bounds.width, height: 110))
self.view.addSubview(scView)
scView.backgroundColor = UIColor.lightGray
scView.translatesAutoresizingMaskIntoConstraints = false
for i in 0 ... 10 {
let myNewView=UIView(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100))
myNewView.backgroundColor=UIColor.white
myNewView.layer.cornerRadius=10
self.scView.addSubview(myNewView)
let subVwOffset = 5
let label = UILabel(frame: CGRect(x: subVwOffset, y: CGFloat(buttonPadding)+5, width: 150, height: 10))
label.textAlignment = .left
label.text = "I'am a title label"
label.textColor = .black
myNewView.addSubview(label)
let ditancelabel = UILabel(frame: CGRect(x: subVwOffset, y: CGFloat(buttonPadding)+5, width: 50, height: 10))
ditancelabel.textAlignment = .right
ditancelabel.text = "0.04 km"
ditancelabel.textColor = .red
myNewView.addSubview(ditancelabel)
let textView = UITextView(frame: CGRect(x: subVwOffset, y: CGFloat(buttonPadding)+15, width: 200.0, height: 40.0))
self.automaticallyAdjustsScrollViewInsets = false
textView.center = self.view.center
textView.textAlignment = NSTextAlignment.justified
textView.textColor = UIColor.lightGray
textView.backgroundColor = UIColor.clear
myNewView.addSubview(textView)
let button = UIButton()
button.tag = i
button.backgroundColor = UIColor.white
button.setTitle("\(i)", for: .normal)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = UIColor.clear
button.addTarget(self, action:#selector(handleRegister(sender:)), for: .touchUpInside)
button.frame = CGRect(x: subVwOffset, y: CGFloat(buttonPadding), width: 200, height: 100)
xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width
myNewView.addSubview(button)
}
scView.contentSize = CGSize(width: xOffset, height: scView.frame.height)
Let me know if you find any other difficulty.

Related

How to specify which child view does not inherit shadows when setting shadows for the parent view?

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)

Two UIButton form one having gradient color and another having border and rounded corner

I am trying to achive this using gradient color and corner radius on [.topLeft,.bottomLeft] and [.topRight,.bottomRight] but I am not able to achieve the border color on one side.
Here is my code
//btnMale gradient color
btnMale.roundCorners([.topLeft,.bottomLeft], radius: 20)
btnFemale.roundCorners([.topRight,.bottomRight], radius: 20)
btnMale.backgroundColor = .clear
let gradient1: CAGradientLayer = CAGradientLayer()
gradient1.frame = CGRect(x: 0, y: 0, width: (self.view.frame.size.width - 90)/2 , height: btnMale.frame.size.height)
gradient1.colors = [Colors.appYellow,Colors.appRed].map { $0.cgColor }
gradient1.startPoint = GradientOrientation.horizontal.startPoint
gradient1.endPoint = GradientOrientation.horizontal.endPoint
btnMale.layer.insertSublayer(gradient1, at: 0)
btnMale.applyGradient(withColours: [Colors.appYellow,Colors.appRed], gradientOrientation: .horizontal)
btnMale.borderWidth = 0.0
btnFemale.borderWidth = 0.5
btnFemale.borderColor = .black
btnMale.setImage(UIImage(named: Images.imgMaleIconWhite), for: .normal)
btnFemale.setImage(UIImage(named: Images.imgFemaleIconBlack), for: .normal)
and here is my output
Please help.
Pradip it's better to add buttons in UIView. It will be easy for you to manage. I made UI like you want using UIView. See this,
let button = UIButton.init(frame: CGRect.init(x: 0, y: 250, width: 200, height: 100))
button.setTitle("HI", for: .normal)
button.addTarget(self, action: #selector(self.btnTapped(pageNo:)), for: .touchUpInside)
self.view.addSubview(button)
let view = UIView.init(frame: CGRect.init(x: 50, y: 250, width: 250, height: 50))
view.backgroundColor = UIColor.red
self.view.addSubview(view)
let btnMale = UIButton.init(type: .custom)
btnMale.frame = CGRect.init(x: 0, y: 0, width: 125, height: 50)
btnMale.setTitle("Male", for: .normal)
btnMale.applyGradient(colours: [UIColor.yellow,UIColor.red])
view.addSubview(btnMale)
let btnFemale = UIButton.init(type: .custom)
btnFemale.frame = CGRect.init(x: 125, y: 0, width: 125, height: 50)
btnFemale.setTitle("Female", for: .normal)
view.addSubview(btnFemale)
view.layer.cornerRadius = 25
view.clipsToBounds = true
Output of above code will look like this.

Swift Add UIView with subviews to titleView

I'd like to customize my navigation bar and insert a UIView with two subviews into the title view of the navigation bar.
Unfortunately the subviews aren't displayed.
Do you have any idea why?
let titleContainer = UIView()
titleContainer.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
let searchIcon = UIButton()
searchIcon.setImage(UIImage(named: "search_icon"), for: UIControlState.normal)
searchIcon.layer.frame = CGRect(x: 0, y: (UIScreen.main.bounds.size.height - 28) / 2, width: 28, height: 28)
titleContainer.addSubview(searchIcon)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 28, y: UIScreen.main.bounds.size.height, width: UIScreen.main.bounds.size.width-28, height: UIScreen.main.bounds.size.height)
titleLabel.textColor = UIColor(red:255, green:255, blue:255, alpha:1.0)
titleContainer.addSubview(self.titleLabel)
self.navigationItem.titleView = titleContainer
problem and expected solution:
Why did you use height of all the element as UIScreen.main.bounds.size.height? Please try the below code.
P.S. I have changed the color of searchIcon as i didn't have the image and changed the color of text of label and added some text so that it is visible.
let titleContainer = UIView()
titleContainer.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 45)
let searchIcon = UIButton()
//searchIcon.setImage(UIImage(named: "search_icon"), for: UIControlState.normal)
searchIcon.backgroundColor = UIColor.red
searchIcon.layer.frame = CGRect(x: 0, y: 8, width: 28, height: 28)
titleContainer.addSubview(searchIcon)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 28, y: 8, width: UIScreen.main.bounds.size.width-28, height: 30)
titleLabel.textColor = UIColor.blue//(red:255, green:255, blue:255, alpha:1.0)
titleContainer.addSubview(titleLabel)
titleLabel.text = "Sample Text"
self.navigationItem.titleView = titleContainer
Here is the screenshot of the above code:
Please change the x,y accordingly as per your requirement.
Have you considered making your let bar = UINavigationBar() programmatically and then add it with: self.view.addSubview(bar)?
So you can push all the "UIBarItems" you want..?

App crashes due to use a custom view with images on different view

My app is working fine with all iOS simulator above iOS v7.0 and also working on most of device but its crash into iPod touch iOS 8.4. I am not able to identify what was the issue with my app, because when I connect this device with Xcode 6.3 and test my app then its working fine.
So my app crash into iPod touch iOS V8.4 in two different cases:
Download app from Test fairy into device and open screen on which I
am using my custom view
Connect device with Xcode and run app into device(With connected
device app work fine even on screen where I used my custom view).
Then disconnect device, Run app again but this time my app was crash
when I reach on screen with custom view.
Custom view code :
import UIKit
class OutfitView:UIView {
var screenSize = SCREEN_SIZE()
let bigScaleImageView: UIImageView!
let rigthSideImageOne: UIImageView!
let rigthSideImageTwo: UIImageView!
let rigthSideImageThree: UIImageView!
let oufitNameLbl: UILabel!
let oufitItemCountLbl: UILabel!
let likeBtn: UIButton!
let likeCountBtn: UIButton!
let rigthSideFooterLbl:UILabel!
let moreImageLbl: UILabel!
override init(frame: CGRect ) {
var yposition = CGFloat(0)
oufitNameLbl = UILabel(frame: CGRect(x: 0, y: yposition, width: (screenSize.width - 20) * (70/100), height: 30))
oufitNameLbl.text = "Outfit Name"
oufitNameLbl.textAlignment = .Left
oufitNameLbl.textColor = UIColor.blackColor()
oufitNameLbl.font = UIFont.systemFontOfSize(18)
oufitItemCountLbl = UILabel(frame: CGRect(x: oufitNameLbl.frame.width, y: yposition, width: (screenSize.width - 20) * (30/100), height: 30))
oufitItemCountLbl.text = "8 Items"
oufitItemCountLbl.textAlignment = .Right
oufitItemCountLbl.textColor = UIColor.grayColor()
oufitItemCountLbl.font = UIFont.systemFontOfSize(14)
yposition += oufitNameLbl.frame.height
var height = frame.height - 80
bigScaleImageView = UIImageView(frame: CGRect(x: 0, y: yposition, width: (screenSize.width - 20) * (70/100) - 4, height: height))
bigScaleImageView.contentMode = UIViewContentMode.ScaleAspectFit
bigScaleImageView.layer.borderWidth = 2
bigScaleImageView.layer.borderColor = UIColor.whiteColor().CGColor
bigScaleImageView.backgroundColor = UIColor.whiteColor()
rigthSideImageOne = UIImageView(frame: CGRect(x: bigScaleImageView.frame.size.width+4, y: yposition, width: (screenSize.width - 20) * (30/100), height: (height - 8)/3))
rigthSideImageOne.layer.borderWidth = 2
rigthSideImageOne.layer.borderColor = UIColor.whiteColor().CGColor
rigthSideImageOne.contentMode = UIViewContentMode.ScaleAspectFit
rigthSideImageOne.backgroundColor = UIColor.whiteColor()
rigthSideImageTwo = UIImageView(frame: CGRect(x: bigScaleImageView.frame.size.width+4, y: yposition + rigthSideImageOne.frame.height+4, width: (screenSize.width - 20) * (30/100), height: (height - 8)/3))
rigthSideImageTwo.layer.borderWidth = 2
rigthSideImageTwo.layer.borderColor = UIColor.whiteColor().CGColor
rigthSideImageTwo.contentMode = UIViewContentMode.ScaleAspectFit
rigthSideImageTwo.backgroundColor = UIColor.whiteColor()
rigthSideImageThree = UIImageView(frame: CGRect(x: bigScaleImageView.frame.size.width+4, y:yposition + (rigthSideImageOne.frame.height + rigthSideImageTwo.frame.height)+8, width: (screenSize.width - 20) * (30/100), height: (height - 8)/3))
rigthSideImageThree.layer.borderWidth = 2
rigthSideImageThree.layer.borderColor = UIColor.whiteColor().CGColor
rigthSideImageThree.contentMode = UIViewContentMode.ScaleAspectFit
rigthSideImageThree.backgroundColor = UIColor.whiteColor()
moreImageLbl = UILabel(frame: CGRect(x: bigScaleImageView.frame.size.width+4, y: yposition + (rigthSideImageOne.frame.height + rigthSideImageTwo.frame.height)+8, width: (screenSize.width - 20) * (30/100), height: (height - 8)/3))
moreImageLbl.text = "+4"
moreImageLbl.textAlignment = .Center
moreImageLbl.textColor = UIColor.whiteColor()
moreImageLbl.backgroundColor = Utilities.getGSBlackColor(alphaValue: CGFloat(0.6))
yposition += bigScaleImageView.frame.height
let editLikeView = UIView(frame: CGRect(x: 0, y: yposition, width: screenSize.width - 20, height: 50))
likeBtn = UIButton(frame: CGRect(x: 0, y: 8, width: 15, height: 15))
likeBtn.setTitle("", forState: .Normal)
likeBtn.setTitle("", forState: .Selected)
//likeBtn.backgroundColor = UIColor.whiteColor()
var image = UIImage(named: "gray_heart")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
likeBtn.setImage(image, forState: .Normal)
likeBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
likeBtn.setTitleColor(Utilities.getGSRedColor(), forState: .Selected)
likeBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
likeBtn.titleLabel?.textAlignment = .Left
likeBtn.userInteractionEnabled = true
editLikeView.addSubview(likeBtn)
likeCountBtn = UIButton(frame: CGRect(x: 13, y: 0, width: 30, height: 30))
likeCountBtn.setTitle("28", forState: .Normal)
likeCountBtn.setTitle("28", forState: .Selected)
likeCountBtn.setTitleColor(Utilities.getMasterColor(), forState: .Normal)
likeCountBtn.setTitleColor(Utilities.getMasterColor(), forState: .Selected)
likeCountBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
likeCountBtn.titleLabel?.textAlignment = .Left
likeCountBtn.userInteractionEnabled = true
editLikeView.addSubview(likeCountBtn)
rigthSideFooterLbl = UILabel(frame: CGRect(x: (screenSize.width-20) - ((screenSize.width - 20) * (0.80)), y: 0, width:(screenSize.width - 20) * (0.80), height: 30) )
rigthSideFooterLbl.text = "CreatedBy:#XXXXXXXXX"
rigthSideFooterLbl.textAlignment = .Right
rigthSideFooterLbl.textColor = UIColor.blackColor()
rigthSideFooterLbl.font = UIFont.systemFontOfSize(16)
editLikeView.addSubview(rigthSideFooterLbl)
super.init(frame: frame)
self.addSubview(oufitNameLbl)
self.addSubview(oufitItemCountLbl)
self.addSubview(editLikeView)
self.addSubview(bigScaleImageView)
self.addSubview(rigthSideImageOne)
self.addSubview(rigthSideImageTwo)
self.addSubview(rigthSideImageThree)
self.addSubview(moreImageLbl)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
And code where I used my custom view:
for i in 1...3{
var outfitView = OutfitView(frame: CGRect(x: 10, y: yposition, width: screenSize.width - 20, height: 330))
outfitView.bigScaleImageView.image = image1
outfitView.rigthSideImageOne.image = image2
outfitView.rigthSideImageTwo.image = image3
outfitView.rigthSideImageThree.image = image4
itemDetailView.frame.size.height += (outfitView.frame.height + 10 )
yposition += outfitView.frame.height + 10
itemDetailView.addSubview(outfitView)
}

Swift Extra argument "width" in call in CGRect

I am trying to add a few buttons in the code
and i get an error "Extra argument "width" in call"
let yPos = txt.frame.origin.y + txt.frame.height
for i in 0..<count {
let yt : Int = i*40 + yPos
//let b = UIButton(frame: CGRect(x: xPos, y: 40, width: 100, height: 30))
var b = UIButton(frame: CGRect(x: xPos, y: yt, width: 100, height: 30))
b.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "test:"))
b.backgroundColor = UIColor.blackColor()
b.tag = i
self.addSubview(b)
}
I cant figure out what is the problem.
I tried to cast all the argument to Int but i get the same error.
Any suggestions?
my mistake i had to cast all the arguments to CGFloat
for i in 0...2 {
let yt : CGFloat = CGFloat(i*40) + yPos
//let b = UIButton(frame: CGRect(x: xPos, y: 40, width: 100, height: 30))
var b = UIButton(frame: CGRect(x: xPos, y: yt, width: 100, height: 30))
b.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "test:"))
b.backgroundColor = UIColor.blackColor()
b.tag = i
self.addSubview(b)
}

Resources