Center a UIImageView over a MKMapView - ios

I have a view that contains both a MKMapView and an UIImageView. The MKMapView is positioned via constraints set on its auto-alignment. For the image view, I would like to programmatically position the center of the image over the center of the image view. Here is what I've tried:
class MyViewController: UIViewController {
#IBOutlet weak var mapView: MKMapView!
#IBOutlet weak var centerPinImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
centerPinImageView.bounds = mapView.bounds
centerPinImageView.center = mapView.center
centerPinImageView.frame = mapView.frame
centerPinImageView.setNeedsDisplay()
}
}
This code does not change the position of the image view. How can I fix this?

Try same code in below method instead of viewDidLoad()
override func viewDidLayoutSubviews() {
}
OR
Add this line for image view,
centerPinImageView.setTranslatesAutoresizingMaskIntoConstraints(true)
Hope it might help you.

If you are using a XIB file why don't you just make sure the MapView and ImageView are siblings and then set centre constraints for the ImageView to match those of the MapView?

Related

Swift: stop image inside scroll from moving up and down

I am working on a sample to display images inside scroll. The image is loading fine but then it allows me to drag the image up and down inside the scroll. I would still want to be able to move right and left (horizontally), but the image should be fixed from moving vertically.
The scroll view and the image constraints are set to the top, bottom, left and right as those of the main view.
What am I doing wrong?
This is my code and storyboard settings
import UIKit
class Image2ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.scrollView.contentSize = (imageView.image?.size)!
}
}
and these are my storyboard settings for ScrollView and ImageView. The top 2 are for the scroll and other 2 are for the image.
Adding this line in the viewDidload solved the issue:
self.automaticallyAdjustsScrollViewInsets = false

How to update constraints after adding UIView from xib on UIScrollView in swift?

I have a UIViewScroll(background color is blue) in view controller. I need a UIView(background color is white) that were from Xib. The Xib view has a UILabel(background color is green) with constraints. Now, the problem is UILabel constraints not applied after adding it to scrollView. How to add UIView without loss of constraints? Refer following screenshots and code.
Note:
I need to just update constraints of the sub views of the UIView without using IBOutlets of NSLayoutConstraints.
UIView on Xib:
Code:
class ViewController: UIViewController {
#IBOutlet var scrollView: UIScrollView!
var profileView:UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.profileView = UINib.init(nibName: "ProfileView", bundle: nil).instantiate(withOwner: self)[0] as! UIView
self.scrollView.addSubview(self.profileView)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.profileView.layer.frame.size = CGSize(width: self.scrollView.frame.width, height: self.profileView.frame.height)
self.profileView.layer.position = CGPoint(x: self.scrollView.frame.width/2, y: (self.profileView.frame.height/2)+10)
}
}
Output:
Update: More Information
I am aware to set contentSize of the scrollView. I used layer properties of UIView for manipulating height and width of the UIView. Instead of changing height and width also I need to update constraints of the sub views of UIView.
This is an example for understanding. But, In real I will be add more views like that.
Github Repository :
https://github.com/RAJAMOHAN-S/ScrollViewTest
Required output:
Your best bet is to set the constraint of self.profileView programmatically, I've added an example below to get you started.
class TestVC: UIViewController {
#IBOutlet var scrollView: UIScrollView!
private var profileView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.profileView = UINib.init(nibName: "ProfileView", bundle: nil).instantiate(withOwner: self)[0] as! UIView
self.configureProfileView()
}
private func configureProfileView() -> Void {
self.profileView.translatesAutoresizingMaskIntoConstraints = false
self.scrollView.addSubview(self.profileView)
self.profileView.widthAnchor.constraint(equalTo: self.scrollView.widthAnchor).isActive = true
self.profileView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
// Pin the profile view to the top of the scrollView
self.profileView.topAnchor.constraint(equalTo: self.scrollView.topAnchor).isActive = true
}
}
More information can be found here too: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html

Why is .center only working in the y axis for me?

class ViewController: UIViewController {
#IBOutlet var friendsButton: UIImageView! //Friends Button
#IBOutlet var circleButton: UIImageView! //Circle Button
#IBOutlet var profileButton: UIImageView! //Profile Button
#IBOutlet var whiteBoxSelector: UIImageView! //White Box Selector
override func viewDidLayoutSubviews() {
whiteBoxSelector.center = self.friendsButton.center //line that doesn't seem to run properly
}
}
Once I run this, my whiteBoxSelector moves to the friends button, but only in the y axis, and the x position seems to be 0. I haven't set any constrains for the white box and my buttons are in a stack view.
The center property of a view specifies:
The center of the frame. The center is specified within the coordinate
system of its superview and is measured in points. Setting this
property changes the values of the frame properties accordingly.
Your issue is that the whiteBoxSelector and friendsButton don't share the same superview, so the coordinate systems aren't the same. You need to translate the center of the friendsButton from the coordinate system of the friendsButton (its superview) to the coordinate system of the whiteBoxSelector (its superview).
override func viewDidLayoutSubviews() {
whiteBoxSelector.center = whiteBoxSelector.superview.convert(friendsButton.center, from: friendsButton.superview)
}

Scrollview just show my last array's element in view which i create with storyboard in swift 3?

I want to add a few view in scrollview. But I dont want to this view programmatically. I want to create with storyboard. This view include an image. I added scrollview and added to view. But in scrollview I just showing my last view. I want to 2 view and 2 image like my view. How can i fix this?
This is my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var mainScrollView: UIScrollView!
var imageArray = [UIImage]()
#IBOutlet weak var myView: UIView!
#IBOutlet weak var imgv: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageArray = ["image","image2"]
for i in 0..<imageArray.count{
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
myView.layer.frame.size.width = myView.frame.width * CGFloat(i + 1)
imgv.image = imageArray[i]
mainScrollView.addSubview(myView)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
This is storyboard and controller looks like
You are having an issue with constraints from what I see and that is why you might not see every element that you put on your storyboard. You need to correct those errors for everything to show up correctly in your scrollview. Keep in mind, you need to tell the scrollview what its height and width should be in the storyboard. They need to be defined in some way. This is a common mistake when working with scrollviews.
You can put a view in your scrollview and define its height and width property. You can set its width to be equal to the screen's width and you can set a constant for your height.

UIView Issue in swift2.0

I have created a uiview in storyboard and have placed it in some random point(autoLayout not applied) . Now in viewDidLoad() Im trying to place it at the center of the screen. But its not moving from that place and replaced at center Why? is this because of some order viewController Life Cycle ? But when I Create a UIView programmatically and placed it in center it works .??
class ViewController: UIViewController {
#IBOutlet weak var view1: UIView!
override func viewDidLoad() {
super.viewDidLoad()
view1.center = view.center
}
}
Your guess is correct. You need to do this in viewWillAppear. viewWillLayoutSubviews is also a good place to put this.
use viewDidLayoutSubviews
- (void) viewDidLayoutSubviews {
self.view1.center = self.view.center
}

Resources