Table View Height restricted Scroll - swift

so I have a table view which is the right size of my screen but the height is wrong because I cann't scroll down to see more parts/cells.
I have set the height in this code:
tableView.frame = CGRect(x: 0, y: 75, width: self.view.frame.width, height: self.view.frame.height)
should I just make the height a really big number or is there a way so that when more cells are filled I can scroll down?

tableView.frame = CGRect(x: 0, y: 75, width: self.view.frame.width, height: self.view.frame.height - 75)
You have add y position at 75. so remove 75 from screen height.

Related

Why is CALayer not being added to UITextView

I have a text view and I am attempting to add a line underneath it. I am trying to accomplish this with a CALayer, however it is not showing up in the textView. I would appreciate it if someone could help me. The code is below.
let border = CALayer()
border.backgroundColor = UIColor(hexString: "#CC0000").cgColor
border.frame = CGRect(x: 0, y: messageField.frame.height - 3, width: messageField.frame.width, height: 3)
messageField.layer.addSublayer(border)
In my test app, I just tried this:
let messageField = UITextView(frame: CGRect(x: 30, y: 40, width: view.frame.width - 60, height: 40))
var border = CALayer()
border.backgroundColor = UIColor.red.cgColor
border.frame = CGRect(x: 0, y: messageField.frame.height - 3, width: messageField.frame.width, height: 3)
messageField.layer.addSublayer(border)
view.addSubview(messageField)
And it worked fine. Weird but fine. The color showed up no problem, but it scrolls along with the text view. But that might be what you want. I tried it with different heights for the message field too. I couldn't get it to not show.
If you want it to work and have the line not scroll with the text, try this answer: https://stackoverflow.com/a/38327895/793607

Central align subView

I'm trying to centre a UIView in another view (so like a pop up view), but whatever I do, I just cannot align it centrally!
func loadPopUpView() {
let customView = CGRect(x: view.center.x, y: view.center.y, width: 100, height: 100)
popUpView = UIView(frame: customView)
popUpView.backgroundColor = UIColor.black
view.addSubview(popUpView)
popUpView.isHidden = false
}
I've changed the background colour to black just so I know when it appears.
I cannot do this with storyboard because it's going on a tableView, so I'm trying to do it programmatically. Result Image
You also need to minus half value of your custom view height and width from x and y like below:
let customView = CGRect(x: view.center.x - 50, y: view.center.y - 50, width: 100, height: 100)
You are telling it to put top left corner in the center, hence you need to let it get half size in position.
let customView = CGRect(x: view.center.x-50, y: view.center.y-50, width: 100, height: 100)
Another solution is the use anchorpoints.
customView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
customView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
customView.heightAnchor.constraint(equalToConstant: 100).isActive = true
customView.widthAnchor.constraint(equalToConstant: 100).isActive = true
On CGRect(x:, y:, width:, height:), the point (x,y) is the origin. In iOS, that's the top left point.
On the CGRect doc:
In the default Core Graphics coordinate space, the origin is located
in the lower-left corner of the rectangle and the rectangle extends
towards the upper-right corner. If the context has a
flipped-coordinate space—often the case on iOS—the origin is in the
upper-left corner and the rectangle extends towards the lower-right
corner.
So to fix this:
let width = 100.0
let height = 100.0
let customViewFrame = CGRect(x: view.center.x - width/2.0, y: view.center.y - height/2.0, width: width, height: height)
Another solution would be to apply the center once the frame (especially the width/size) have been set.
let customViewFrame = CGRect(x: 0, y: 0, width: 100, height: 100)
customViewFrame = view.center

Change UIImageView size programmatically without changing position

I want to chance a UIImageView size without changing the UIImageView Location. This is what i got so far:
Person_1.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
I don't want the x and y to be 0. I want the UIImageView on the same place but bigger. Please help!
If you must do it by way of changing the frame, then it would look something like:
Person_1.frame = CGRect(x: Person_1.frame.origin.x, y: Person_1.frame.origin.y, width: 200, height: 200)
Is there a reason you wouldn't be doing it by way of constraints/AutoLayout?

Navigationbar height increase and decrease in ios

i am new to swift and here i have a requirement i want to increase/ decrease navigation bar height as per requirement
Here: To increase navigationbar height i used this lines
extension UINavigationBar {
override open func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.size.width, height: 99.0)
}
}
this lines are working .but how to decrease navigationbar height
Thank you in advance
You can use this sample project.
https://developer.apple.com/library/content/samplecode/NavBar/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007418
Why don't you just simply set the frame for your navigationBar?
let size = CGSize(width: UIScreen.main.bounds.size.width, height: 100)
navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)

Position of UIView in ScrollView is wrong

I tried to add 4 views to scrollview to create horizontal paging view.
But I don't understand why between each view have white space which width equal width of each view. They should be close together.
walkthroughItems = {
let width = UIScreen.mainScreen().bounds.size.width
let height = UIScreen.mainScreen().bounds.size.height
return [
WalkthroughItem0(frame: CGRect(x: 0, y: 0, width: width,height: height)),
WalkthroughItem1(frame: CGRect(x: width, y: 0, width: width,height: height)),
WalkthroughItem2(frame: CGRect(x: width * 2, y: 0, width: width,height: height)),
WalkthroughItem3(frame: CGRect(x: width * 3, y: 0, width: width,height: height))
]
}()
for item in walkthroughItems {
scrollView.addSubview(item)
}
scrollView.contentSize = CGSize(width: UIScreen.mainScreen().bounds.size.width*4, height: UIScreen.mainScreen().bounds.size.height)