Saturday 15 September 2012

ios - How to center my imageView inside another ImageView programmatically -



ios - How to center my imageView inside another ImageView programmatically -

hi first post. helping.

i created uiimageview , added blur effect on it. added uiimageview subview of blurred image view. trying figure out how can create height , width proportional programmatically on screen sizes

i figured out how center cant size good. want height little smaller height of superview , circle width equal height.

//create blurred profile image //first create uiimageview allow blurprofilepic = uiimageview(frame: cgrectmake(0, 64, view.frame.size.width, view.frame.size.height/3.0)) //assign value blurprofilepic.image = uiimage(named: "placeholder") //make sure stays within created bounds blurprofilepic.layer.maskstobounds = true //center image blurprofilepic.contentmode = uiviewcontentmode.center //create blur effect var visualeffectview = uivisualeffectview(effect: uiblureffect(style: .light)) uivisualeffectview visualeffectview.frame = blurprofilepic.bounds //add blur layer blurprofilepic blurprofilepic.addsubview(visualeffectview) //create real profile pic //arbitrary frame allow profilepic = uiimageview(frame: cgrectmake(50, 50, 180, 180)) //the 180,180 needs changed sized proportionally screens //center within blurimage profilepic.center = cgpointmake(blurprofilepic.frame.size.width/2, blurprofilepic.frame.size.height/2) profilepic.image = uiimage(named: "placeholder") profilepic.layer.borderwidth = 5 profilepic.layer.bordercolor = uicolor.whitecolor().cgcolor profilepic.layer.cornerradius = profilepic.frame.size.width/2 profilepic.clipstobounds = true blurprofilepic.addsubview(profilepic) view.addsubview(blurprofilepic)

does help you?

class viewcontroller: uiviewcontroller { allow blurprofilepic = uiimageview() allow profilepic = uiimageview() override func viewwilllayoutsubviews() { blurprofilepic.frame = cgrect(x: 0, y: uiapplication.sharedapplication().statusbarframe.height, width: view.bounds.width, height: view.bounds.height / 3) allow blurprofilepicbounds = blurprofilepic.bounds profilepic.frame.size = cgsize(width: blurprofilepicbounds.height, height: blurprofilepicbounds.height) profilepic.center = cgpointmake(blurprofilepicbounds.width / 2, blurprofilepicbounds.height / 2) profilepic.layer.cornerradius = profilepic.frame.size.width / 2 } override func viewdidload() { super.viewdidload() //assign value blurprofilepic.image = uiimage(named: "placeholder") //make sure stays within created bounds blurprofilepic.layer.maskstobounds = true //center image blurprofilepic.contentmode = .scaleaspectfill //create blur effect var visualeffectview = uivisualeffectview(effect: uiblureffect(style: .light)) uivisualeffectview visualeffectview.frame = blurprofilepic.bounds visualeffectview.autoresizingmask = .flexiblewidth | .flexibleheight //add blur layer blurprofilepic blurprofilepic.addsubview(visualeffectview) profilepic.image = uiimage(named: "placeholder") profilepic.contentmode = .scaleaspectfill profilepic.layer.borderwidth = 5 profilepic.layer.bordercolor = uicolor.whitecolor().cgcolor profilepic.clipstobounds = true blurprofilepic.addsubview(profilepic) view.addsubview(blurprofilepic) } }

update:

this snapshot when set profilepic.contentmode , blurprofilepic.contemode both .scaleaspectfill, seems

if set profilepic.contentmode .scaleaspectfit, can see image not resized fill circle.

ios iphone swift

No comments:

Post a Comment