I'm trying to figure out the difference between frame and bounds in iOS, basically the Bounds refers to the views own coordinate system while Frame refers to the views parent coordinate system. But, I have a viewA and a viewB, ViewA is the parent of ViewB, if I change the bounds of ViewA from (0,0,100,100) -> (50,0,100,100), viewB shold move to right because it's coordinate system move to right by 10 right? but ViewB move to left! See the code and behavior:
class ViewController: UIViewController {
@IBOutlet weak var viewA: UIView!
@IBOutlet weak var viewB: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func buttonClicked(_ sender: Any) {
viewA.layer.bounds = CGRectMake(50, 0, 240, 377)
}
}
Shouldn't the center view move to right not left because we +30?


If you head over to Google (or your favorite search engine) and search for
UIView frame vs boundsyou'll find lots and lots of discussion, along with plenty of articles/blogs that go into great detail on the differences.But -- to give you a quick answer to why your view moves left instead of right (as you expected)...
Suppose I have a 200x200
UIView. I add a 400x200UIImageViewas a subview, with this image:and I constrain the image view Top and Leading to the "container" view (make sure
.clipsToBounds = trueon the container view).It will look like this (on a yellow background):
which should be no surprise.
Now, let's change the container view's bounds to:
we will see this:
The image view does not get "pushed" 50-points to the right... nor does it get "pulled" 50-points to the left.
What we've done is told the container view to: "display the
boundsrectangle from your content (your subview(s))":That's how a
UIScrollViewworks... if the scroll view's content is larger than the scroll view's frame, we can drag the content... which changes the scroll view's.contentOffsetwhich, in turn, changes the scroll view'sbounds.Quick
UIScrollViewexample showing that:Looks like this as I scroll to the left:
Here is about the same thing, but instead of a
UIScrollViewwe'll use a "container"UIView... each tap anywhere will increment the container view's.bounds.origin.xby 20-points:Of course, this has only addressed your specific condition. If you spend some time reading up on
frame vs boundsyou'll see that it comes into play in a much more critical context when you start applying scaling and rotational transforms to your views.