UIScrollView starts out halfway down content [duplicate] - ios

This question already has answers here:
Why is UIScrollView leaving space on top and does not scroll to the bottom
(13 answers)
Closed 6 years ago.
I am trying to implement a UIScrollView in a similar fashion to the featured banner at the top of the App Store. I am adding 3 views and then paging through them using the code below. When the controller loads however, it is started with the content down a bit. If I tap on the view the content goes back into where it should be. How can I fix this? I've trying setting the content offset to 0, I've tried manually scrolling to the origin rect, and I've tried putting my views in a content view, but nothing has worked.
featuredScrollView.alwaysBounceVertical = false
featuredScrollView.contentSize = CGSize(width: 3 * featuredScrollView.frame.size.width, height: featuredScrollView.frame.size.height)
let contentView = UIView(frame: CGRect(x: 0, y: 0, width: 3 * featuredScrollView.frame.size.width, height: featuredScrollView.frame.size.height))
featuredScrollView.addSubview(contentView)
for i in 0..<3
{
let testView = UIView(frame: CGRect(x: CGFloat(i) * featuredScrollView.bounds.size.width, y: 0, width: featuredScrollView.bounds.size.width, height: featuredScrollView.bounds.size.height))
testView.backgroundColor = .blue
testView.layer.borderColor = UIColor.white.cgColor
testView.layer.borderWidth = 1
contentView.addSubview(testView)
}

Did you try set automaticallyAdjustsScrollViewInsets to NO?

Related

Centering subview on ViewController

Simply as the title says, I'm trying to center one Activity Indicator on the center of my View by doing that:
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
self.view.addSubview(activityIndicator)
but I end up with this result:
the problem persists within all view controllers in this app, except for the last one, that really centers it as you can see:
Xcode 10 / Swift 4
It can be the hight of the tab bar is disturbing. anyways,
If your code (.center) doesn't work, try this:
activityIndicator.frame = CGRect.init(x: view.frame.size.width / 2, y: view.frame.size.height / 2, width: 0, height: 0)
If it still doesn't work, change y: view.frame.size.height / 2 - "THE HIGHT OF TAB BAR".

How to draw a partial border?

Just as the following image, how to draw that black line?
I want the line to be drawn on layer, not on another view.
The problem I got is how to locate the bottom. Thanks for any suggestion.
Here i have used textfield same way you can use for button also
let borderOld = CALayer()
let width = CGFloat(1.5)
borderOld.frame = CGRect(x: 0, y: txtField.frame.size.height - width, width: txtField.frame.size.width, height: txtField.frame.size.height)
borderOld.borderWidth = width
txtField.layer.masksToBounds = true
txtField.layer.addSublayer(borderOld)

iOS - UITextField: extend the bottom line for ANY resolution

I created a UITextFiled with a bottom line using this:
let Bottomline CALayer = ()
bottomLine.frame CGRect = (x: 0, y: usernameTextField.frame.height-7, width: usernameTextField.frame.width, height: 1)
bottomLine.backgroundColor = UIColor.black.cgColor
TextField.borderStyle = UITextBorderStyle.none
TextField.layer.addSublayer (Bottomline)
and the result of an iPhone 6 (right) is this:
Ok.
✄------------------------
The problem is to run the same application on a Pro iPad, because the
bottom line does not extend following the UITextField, but is shorter
This is the result on iPad Pro:
I do not understand why the bottom line does not follow the UITextField. When I called the bottom line I defined as:
bottomLine.frame CGRect = (x: 0, y: usernameTextField.frame.height-7, width: usernameTextField.frame.width, height: 1)
I have specified that the length of the line at the bottom must be:
width: usernameTextField.frame.width
What's the problem?
EDIT 1: The contrains are correct, because the UITextField adapts to
all types of resolution
EDIT:2 Thanks Matt! Now work!!!
I do not understand why the bottom line does not follow the UITextField
Because it's a layer. Layers do not automatically change size when their superlayer (the text field) changes size.
So, you need to redraw the bottom line every time the text field changes size.
At the moment, though, you are configuring the "bottom line" layer in viewDidLoad. So you are basing it on the frame that the text field has at that moment. But the text field has not yet attained its real size. Then it does change size, and meanwhile your "bottom line" layer just sits there — so now it is the wrong size.
An easy solution is to subclass UITextField and redraw the line every time layoutSubviews is called:
class MyTextField : UITextField {
var lineLayer : CALayer?
override func layoutSubviews() {
super.layoutSubviews()
self.lineLayer?.removeFromSuperlayer()
let bottomLine = CALayer()
bottomLine.frame = CGRect(x: 0, y: self.bounds.height-7, width: self.bounds.width, height: 1)
bottomLine.backgroundColor = UIColor.black.cgColor
self.layer.addSublayer(bottomLine)
self.lineLayer = bottomLine
}
}
If your text field is a MyTextField, it will behave exactly as you desire.

UITextField custom underline in Swift

I have a question about making an underline on UITextField.
I am trying to make an underline with bar on each end as shown below.
I tried the following and got this one. There is no bar on the right end.
extension UITextField {
func underline() {
let borderWidth = CGFloat(1.0)
let endBorderHeight = CGFloat(10.0)
let bottom = CALayer()
bottom.frame = CGRect(
x: 1,
y: self.frame.height - borderWidth,
width: self.frame.width - 2,
height: borderWidth)
bottom.borderWidth = borderWidth
bottom.borderColor = UIColor.lightGrayColor().CGColor
let leftEndBorder = CALayer()
leftEndBorder.frame = CGRect(
x: 0,
y: self.frame.height - endBorderHeight,
width: borderWidth,
height: endBorderHeight)
leftEndBorder.borderWidth = borderWidth
leftEndBorder.borderColor = UIColor.lightGrayColor().CGColor
print(bottom.frame.width)
let rightEndBorder = CALayer()
rightEndBorder.frame = CGRect(
x: self.frame.width - 1,
y: self.frame.height - endBorderHeight,
width: borderWidth,
height: endBorderHeight)
rightEndBorder.borderWidth = borderWidth
rightEndBorder.borderColor = UIColor.lightGrayColor().CGColor
self.layer.addSublayer(leftEndBorder)
self.layer.addSublayer(bottom)
self.layer.addSublayer(rightEndBorder)
self.layer.masksToBounds = true
}
}
I could make the bar on left side but having trouble making the right side because of the wrong x position of the rightEndBorder probably?
Can anyone tell me what I am doing wrong??
----- edit
I tried to set the x-position of the rightEndBorder to 200 and it gave me the following.
But if I tried to set it to 300, I don't see it anymore.
----- edit
Checked if the entire textfield was shown on the screen.
----- edit
It was that the leading and trailing constraints that changed the width of the textField I guess.
----- Solution
The problem was that I had leading and trailing constraints on the textField and those constraints changed the width after the unline was inserted. After searching google, I figured that I had to make the underline after the constraints were applied which is in the function viewDidLayoutSubviews().
Re-typing from comments section as an answer.
The problem seems to be either that the view is getting cut off (thus you will not see the right border) or the right end border is being shifted farther right after setting. Reason for suspecting this is from your picture where the right border is placed at x = 200 because it is well past half along the bottom border line but should be only be 40% of the way along the line.
Update:
The correct answer to this question was the constraints imposed caused the textfield width to change.

Sizing of Live Views in Swift Playground

I'm having trouble figuring out how to lay out views in Swift Playgrounds for iPad, though this may also be relevant to Mac users.
The following code should create a view with a red square (also a view) that is near the edges of its' super view, but not touching them.
let v = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
let sqv = UIView(frame: CGRect(x: 400, y: 400, width: 50, height: 50))
sqv.backgroundColor = .red
v.addSubview(sqv)
PlaygroundPage.current.liveView = v
The result is not what you'd expect:
I suspect I know what is going on here; live views are at a fixed size that is larger than the display area. Some characteristics of the view are ignored when it is acting as the live view. However, I can't find where this is mentioned in the documentation, which vexes me. More importantly, how do I deal with this? I would like to be able to layout simple UIs that change to fit the current size of the live view. I don't know how to address this issue without trial & error and hardcoding, which are two things I would really like to avoid.
I suspect I know what is going on here; live views are at a fixed size that is larger than the display area.
Actually it's more like the other way around. An iPad screen is 1024 points wide (in landscape orientation). The right-hand pane (where it shows your live view) is 512 points wide. The playground forces your root view (v) to fill that pane, inset by 40 points on the left, right, and top (and more on the bottom). So your root view's width is forced to 432 ( = 512 - 2 * 40), less than the 500 you specified.
Views created in code (like yours) have translatesAutoresizingMaskIntoConstraints = true, and a resizing mask of 0, which means don't adjust the view's frame at all when its parent is resized. So the playground resizes your root view to width 432, but your root view doesn't move or resize its subview (sqv).
The easiest fix is to set the autoresizing mask of the subview to express your intent that it remain near the right and bottom edges of the root view. That means it should have flexible top and left margins:
let v = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
let sqv = UIView(frame: CGRect(x: 400, y: 400, width: 50, height: 50))
sqv.autoresizingMask = [.flexibleLeftMargin, .flexibleTopMargin]
sqv.backgroundColor = .red
v.addSubview(sqv)
PlaygroundPage.current.liveView = v
Result:
let sqv = UIView(frame: CGRect(x: UIScreen.mainScreen().bounds.width-50-1, y:400, width: 50, height: 50))
The above code places your subview 1 point away from the right of the main view. Try changing the value 1 after 50 in x to desired value.

Resources