How can I constrain a subview so that it cannot be dragged outside of it's parent view's b


How to constrain the position of a subview within its parent view. I have a subview within a UIView. The subview can be dragged via gesture recognizer. How can I constrain the subview so that it cannot be dragged outside of it's parent view's bounds.


to constrain a draggable view, you need to check for its position while you move it and then force it to the constrained position if it exceeds the boundaries. So assuming you use touchesMoved :

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { ... CGRect frame = mySubview.frame; if (frame.origin.x < boundaryX) { //frame exceeds the horizontal boundary frame.origin.x = boundaryX; mySubview.frame = frame; } }

so assuming boundaryX is the origin of the parent view then this way the subview will never exceed that boundary. You need to do the same for origin y and x + width and y + height to do constraints from all sides.

hope this helps.


  • New Google Account not able to access script.google.com
  • Method `mul` has an incompatible type for trait
  • Model-Identifier for Node in JavaFX 2 TreeItem
  • Transform matrix of 3D positions with corresponding transformation matrix
  • PivotTable how to set all items in filter to false?
  • How do I specify my own icons so they show up in a Google Endpoints API discovery document?
  • How do I implement getConnection() in DataSource in Java?
  • Clipping path in SVG not working in Safari
  • TryParse SSIS Ignore Source Row
  • Facebook iOS app not launching my app for Applinks enabled link
  • Inject module dependency (like plugin) only if needed AngularJS
  • Does the jQuery $(window).load(); event not fire on pages without a declaration? (…in a chrome exte
  • igraph/visNetwork with R: How to disable forward linking?
  • How can I implement a JavaScript that submit this form according to the clicket button? [duplicate]
  • Purpose of /var/resource_config.json
  • Codeigniter Google Maps API V3 Geocoding not working
  • python garbage collection and _ underscore in interpreter environment
  • How to save a image coded in Data-URI?
  • .NET COM assembly interacting with Excel via BackgroundWorker
  • Is there a modern ( e.g. CLR ) replacement for bison / yacc?
  • Get frequency distribution of a decimal range in MySQL
  • How to add a progress ring to the splash screen in Windows 8?
  • Reference after posting data from web worker
  • search bar getting disappeared in ios UIsearchcontroller
  • pass sessionid through jquery ajax call to php
  • build an API for streaming audio/video from GridFS mongodb
  • Android NDK refer to external libraries in JNI
  • Remove stopwords and tolower function slow on a Corpus in R
  • How to sort by Lucene.Net field and ignore common stop words such as 'a' and 'the
  • How can i use JQuery fadeTo() in IE 7?
  • Why am I getting an Argument exception when creating event handler dynamically?
  • ASP.NET GridView throws: The version of SQL Server in use does not support datatype 'date'
  • How to add html image in to velocity template file to send email?
  • Sign a Pdf using custom digital signature in Java
  • Spring Boot not autowiring @Repository
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}